<html>
<head>
	<title>Programming with SQL Relay using the ADO.NET API</title>
	<link rel="stylesheet" href="../css/styles.css">
</head>
<body>
<h1>Programming with SQL Relay using the ADO.NET API</h1>

<ul>
  <li><a href="#languages">Language Compatibility</a></li>
  <li><a href="#session">Establishing a Session</a></li>
  <li><a href="#query">Executing Queries</a></li>
  <li><a href="#commit">Commits and Rollbacks</a></li>
  <li><a href="#temptables">Temporary Tables</a></li>
  <li><a href="#errors">Catching Errors</a></li>
  <li><a href="#bindvars">Bind Variables</a></li>
  <li><a href="#rebinding">Re-Binding and Re-Executing</a></li>
  <li><a href="#fields">Accessing Fields in the Result Set</a></li>
  <li><a href="#largeresultsets">Dealing With Large Result Sets</a></li>
  <li><a href="#cursors">Cursors</a></li>
  <li><a href="#columns">Getting Column Information</a></li>
  <li><a href="#storedprocedures">Stored Procedures</a></li>
  <li><a href="#extensions">Extensions</a></li>
  <li><a href="#lastinsertid">Getting the Last Insert ID</a></li>
</ul>

<a name="languages"/><h2>Language Compatibility</h2>

<p>The SQL Relay ADO.NET Adapter is supported on Windows platforms using Visual Studio and on Unix/Linux using Mono.</p>

<p>The example code below is given in C# but the SQL Relay ADO.NET Adapter can be used from any language capable of using the .NET API.</p>

<a name="compiling"/><h2>Compiling an SQL Relay Client Program</h2>

<p>When writing an SQL Relay client program using the ADO.NET API, you need to
use the SQLRClient namespace.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#0000FF">using</font></b> SQLRClient<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>You'll also need to include the SQLRelay.dll assembly.  This is usually found in C:\Program Files\Firstworks\bin on Windows or /usr/local/firstworks/lib on Unix/Linux.</p>

<p>As the ADO.NET Adapter ultimately relies on the C API, on Unix/Linux, there is also a SQLRelay.dll.config file, found in the same directory, that maps internal references to libsqlrclient.dll to the Unix/Linux equivalent shared object library.  This file isn't necessary on Windows and isn't installed.</p>

<p>To compile a progarm using the Visual Studio IDE, you just have to configure your project to include the SQLRelay.dll assembly and compile your program.</p>

<p>To compile from the command line using either Visual Studio or Mono, it's easiest to copy SQLRelay.dll into the current directory and build against it locally.</p>

<p>When using Mono, you should also copy SQLRelay.dll.config into the current directory.</p>

<p>For example, to create the executable sqlrexample.exe from the source code sqlrexample.cs...</p>

<p>From the Visual Studio command line:</p>

<blockquote>
  <pre>copy "C:\Program Files\Firstworks\bin\SQLRClient.dll" .
csc /out:sqlrexample.exe sqlrexample.cs /reference:SQLRClient.dll
</pre>

</blockquote>
<p>Using the Mono compiler from the Unix/Linux command line:</p>

<blockquote>
  <pre>cp /usr/local/firstworks/lib/SQLRClient.dll .
cp /usr/local/firstworks/lib/SQLRClient.dll.config .
mcs -pkg:dotnet /out:sqlrexample.exe sqlrexample.cs /reference:SQLRClient.dll
</pre>

</blockquote>
<p>(Note that an explicit reference to the dotnet package is required with Mono).</p>

<p>To run the program under Windows, you can just run it directly:</p>

<blockquote>
  <pre>sqlrexample.exe
</pre>

</blockquote>
<p>Use the mono runtime to run the program on Unix/Linux:</p>

<blockquote>
  <pre>mono sqlrexample.exe
</pre>

</blockquote>
<p>If you get an error about libsqlrclient.dll not being found, then you probably forgot to copy SQLRClient.dll.config into the current directory.</p>

<a name="session"/><h2>Establishing a Session</h2>

<p>To use SQL Relay, you have to first open a connection.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#0000FF">using</font></b> System<font color="#990000">;</font>
<b><font color="#0000FF">using</font></b> SQLRClient<font color="#990000">;</font>
<b><font color="#0000FF">using</font></b> System<font color="#990000">.</font>Data<font color="#990000">;</font>
<b><font color="#0000FF">using</font></b> System<font color="#990000">.</font>IO<font color="#990000">;</font>

<b><font color="#0000FF">namespace</font></b> SQLRExamples
<font color="#FF0000">{</font>
	<b><font color="#0000FF">class</font></b> <font color="#008080">SQLRExample</font>
	<font color="#FF0000">{</font>
		<b><font color="#0000FF">public</font></b> <b><font color="#0000FF">static</font></b> <font color="#009900">void</font> <b><font color="#000000">Main</font></b><font color="#990000">()</font>
		<font color="#FF0000">{</font>
			<font color="#008080">SQLRelayConnection</font> sqlrcon <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">SQLRelayConnection</font></b><font color="#990000">(</font><font color="#FF0000">"Data Source=sqlrserver:9000;User ID=test;Password=test;Retry Time=0;Tries=1;Debug=false"</font><font color="#990000">);</font>
			sqlrcon<font color="#990000">.</font><b><font color="#000000">Open</font></b><font color="#990000">();</font>
		<font color="#FF0000">}</font>
	<font color="#FF0000">}</font>
<font color="#FF0000">}</font>
</tt></pre>

</blockquote>
<p>The following connect string variables control attributes of the connection:</p>

<ul>
  <li><b>Data Source</b> - The SQL Relay server to connect to.  This may be specified as <i>host:port</i>, <i>host:port:socket</i> or just <i>socket</i>.  If host, port and socket are all three specified, then a connection will first be attempted to the local socket and then to the host/port.</li>
  <li><b>User ID</b> - The username to use when logging into SQL Relay.</li>
  <li><b>Password</b> - The password to use when logging into SQL Relay.</li>
  <li><b>Retry Time</b> - If a connection fails, it will be retried on this interval (in seconds).</li>
  <li><b>Tries</b> - If a connection fails, it will be retried this many times.</li>
  <li><b>Initial Catalog</b> - The database/schema to switch to after logging in.  Optional.</li>
  <li><b>Debug</b> - If this is set to true then debug is enabled.</li>
</ul>

<p>In addition, the following connect string variables can be used to establish Kerberos or Active Directory encryption and authentication with the server:</p>

<p>See the <a href="../admin/configguide.html#krb">SQL Relay Configuration Guide</a> for more information about Kerberos and Active Directory configurations.  In particular, User ID and Password are not typically used when using Kerberos/AD.</p>

<ul>
  <li><b>Krb</b> - Set to <b>yes</b> to enable Kerberos/AD encryption/authentication or <b>no</b> to disable it.</li>
  <li><b>Krbservice</b> - Specifies the Kerberos service name of the SQL Relay server.  If omitted or left empty then the service name "sqlrelay" will be used. "sqlrelay" is the default service name of the SQL Relay server.  Note that on Windows platforms the service name must be fully qualified, including the host and realm name.  For example: "sqlrelay/sqlrserver.firstworks.com@AD.FIRSTWORKS.COM".</li>
  <li><b>Krbmech</b> - Specifies the Kerberos mechanism to use.  On Linux/Unix platforms, this should be a string representation of the mechnaism's OID, such as: { 1 2 840 113554 1 2 2 }.  On Windows platforms, this should be a string like: Kerberos.  If omitted or left empty then the default mechanism will be used.  Only set this if you know that you have a good reason to.</li>
  <li><b>Krbflags</b> - Specifies what Kerberos flags to use.  Multiple flags may be specified, separated by commas.  If left empty or NULL then a defalt set of flags will be used.  Only set this if you know that you have a good reason to.  Valid flags include: GSS_C_MUTUAL_FLAG, GSS_C_REPLAY_FLAG, GSS_C_SEQUENCE_FLAG, GSS_C_CONF_FLAG, and GSS_C_INTEG_FLAG.  For a full list of flags, consult the GSSAPI documentation, though note that only the previously listed flags are supported on Windows.</li>
</ul>

<p>The following connect string variables can be used to establish TLS/SSL encryption and authentication with the server:</p>

<p>See the <a href="../admin/configguide.html#tls">SQL Relay Configuration Guide</a> for more information about TLS/SSL configurations.</p>

<ul>
  <li><b>Tls</b> - Set to <b>yes</b> to enable TLS/SSL encryption/authentication or <b>no</b> to disable it.</li>
  <li><b>Tlsversion</b> - Specifies the TLS/SSL protocol version that the client will attempt to use.  Valid values include SSL2, SSL3, TLS1, TLS1.1, TLS1.2 or any more recent version of TLS, as supported by and enabled in the underlying TLS/SSL library.  If omittted or left empty then the highest supported version will be negotiated.</li>
  <li><b>Tlscert</b> - Specifies the file name of the certificate chain file to send to the SQL Relay server.  This is only necessary if the SQL Relay server is configured to authenticate and authorize clients by certificate.</li>
  <li><b>Tlspassword</b> - If Tlscert contains a password-protected private key, then Tlspassword may be supplied to access it.  If the private key is not password-protected, then this argument is ignored, and may be omitted or left empty.</li>
  <li><b>Tlsciphers</b> - Specifies a list of ciphers to allow.  Ciphers may be separated by spaces, commas, or colons.  If omitted or left empty then a default set is used.  For a list of valid ciphers on Linux/Unix platforms, see: man ciphers. For a list of valid ciphers on Windows platforms, see <a target="_blank" href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa375549%28v=vs.85%29.aspx">this page</a>.  On Windows platforms, the ciphers (alg_id's) should omit CALG_ and may be given with underscores or dashes.  For example: 3DES_112.  Only set this if you know that you have a good reason to.</li>
  <li><b>Tlsvalidate</b> - Indicates whether to validate the SQL Relay server's certificate, and may be set to one of the following: <b>no</b> - Don't validate the server's certificate.  <b>ca</b> - Validate that the server's certificate was signed by a trusted certificate authority.  <b>ca+host</b> - Perform "ca" validation and also validate that one of the subject altenate names (or the common name if no SANs are present) in the certificate matches the host provided in the Data Source parameter.  (Falls back to "ca" validation when a unix socket is used.)  <b>ca+domain</b> - Perform "ca" validation and also validate that the domain name of one of the subject alternate names (or the common name if no SANs are present) in the certificate matches the domain name of the host provided in the Data Source parameter.  (Falls back to "ca" validation when a unix socket is used.)</li>
  <li><b>Tlsca</b> - Specifies the location of a certificate authority file to use, in addition to the system's root certificates, when validating the SQL Relay server's certificate.  This is useful if the SQL Relay server's certificate is self-signed.  On Windows, "ca" must be a file name.  On non-Windows systems, "ca" can be either a file or  directory name.  If it is a directory name, then all certificate authority files found in that directory will be used.  If it a file name, then only that file will be used.</li>
</ul>

<p>Note that the supported Tlscert and Tlsca file formats may vary between platforms.  A variety of file formats are generally supported on Linux/Unix platfoms (.pem, .pfx, etc.) but only the .pfx format is currently supported on Windows.</p>

<p>After opening  the connection, a session is established when the first 
query is run.</p>

<p>For the duration of the session, the client occupies one of the database
connections, so care should be taken to minimize the length of a
session.</p>

<a name="query"/><h2>Executing Queries</h2>

<p>There are three ways to execute queries.</p>

<p>If a query doesn't return a result set, such as DML (insert, update, delete, etc.) or DDL (create, drop, etc.) then you can use ExecuteNonQuery.  If the query returns a single value you can use ExecuteScalar.  If the query returns a result set then you must use ExecuteReader.</p>

<p>In any case, you must create an instance of SQLRelayCommand and set its CommandText.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#0000FF">using</font></b> System<font color="#990000">;</font>
<b><font color="#0000FF">using</font></b> SQLRClient<font color="#990000">;</font>
<b><font color="#0000FF">using</font></b> System<font color="#990000">.</font>Data<font color="#990000">;</font>
<b><font color="#0000FF">using</font></b> System<font color="#990000">.</font>IO<font color="#990000">;</font>

<b><font color="#0000FF">namespace</font></b> SQLRExamples
<font color="#FF0000">{</font>
	<b><font color="#0000FF">class</font></b> <font color="#008080">SQLRExample</font>
	<font color="#FF0000">{</font>
		<b><font color="#0000FF">public</font></b> <b><font color="#0000FF">static</font></b> <font color="#009900">void</font> <b><font color="#000000">Main</font></b><font color="#990000">()</font>
		<font color="#FF0000">{</font>
			<font color="#008080">SQLRelayConnection</font> sqlrcon <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">SQLRelayConnection</font></b><font color="#990000">(</font><font color="#FF0000">"Data Source=sqlrserver:9000;User ID=test;Password=test;Retry Time=0;Tries=1;Debug=false"</font><font color="#990000">);</font>
			sqlrcon<font color="#990000">.</font><b><font color="#000000">Open</font></b><font color="#990000">();</font>


			<i><font color="#9A1900">// DML queries...</font></i>
			<font color="#008080">SQLRelayCommand</font> sqlrcom <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayComand<font color="#990000">)</font>sqlrcon<font color="#990000">.</font><b><font color="#000000">CreateCommand</font></b><font color="#990000">();</font>
			sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"insert into testtable values (1,'hello')"</font><font color="#990000">;</font>
			<b><font color="#0000FF">try</font></b>
			<font color="#FF0000">{</font>
				sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteNonQuery</font></b><font color="#990000">();</font>
			<font color="#FF0000">}</font>
			<b><font color="#0000FF">catch</font></b> <font color="#990000">(</font><font color="#008080">Exception</font> ex<font color="#990000">)</font>
			<font color="#FF0000">{</font>
				<font color="#990000">...</font> <font color="#008080">handle</font> exceptions <font color="#990000">...</font>
			<font color="#FF0000">}</font>


			<i><font color="#9A1900">// Single values</font></i>
			sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"select 1 from dual"</font><font color="#990000">;</font>
			<b><font color="#0000FF">try</font></b>
			<font color="#FF0000">{</font>
				<font color="#008080">Int64</font> value <font color="#990000">=</font> sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteScalar</font></b><font color="#990000">();</font>

				<font color="#990000">...</font> <b><font color="#0000FF">do</font></b> something with <font color="#008080">the</font> value <font color="#990000">...</font>
			<font color="#FF0000">}</font>
			<b><font color="#0000FF">catch</font></b> <font color="#990000">(</font><font color="#008080">Exception</font> ex<font color="#990000">)</font>
			<font color="#FF0000">{</font>
				<font color="#990000">...</font> <font color="#008080">handle</font> exceptions <font color="#990000">...</font>
			<font color="#FF0000">}</font>


			<i><font color="#9A1900">// Multiple rows</font></i>
			sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"select * from testtable"</font><font color="#990000">;</font>
			<b><font color="#0000FF">try</font></b>
			<font color="#FF0000">{</font>
				System<font color="#990000">.</font>Data<font color="#990000">.</font><font color="#008080">IDataReader</font> datareader <font color="#990000">=</font> sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteReader</font></b><font color="#990000">();</font>

				<font color="#990000">...</font> <b><font color="#0000FF">do</font></b> something with the <font color="#008080">result</font> set <font color="#990000">...</font>
			<font color="#FF0000">}</font>
			<b><font color="#0000FF">catch</font></b> <font color="#990000">(</font><font color="#008080">Exception</font> ex<font color="#990000">)</font>
			<font color="#FF0000">{</font>
				<font color="#990000">...</font> <font color="#008080">handle</font> exceptions <font color="#990000">...</font>
			<font color="#FF0000">}</font>
		<font color="#FF0000">}</font>
	<font color="#FF0000">}</font>
<font color="#FF0000">}</font>
</tt></pre>

</blockquote>
<a name="commit"/><h2>Commits and Rollbacks</h2>

<p>If you need to execute a commit or rollback, you should create an instance of the SQLRelayTransaction class and use its commit() and rollback() methods rather than sending "commit" or "rollback" queries.  There are two reasons for this.  First, it's much more efficient to call the methods.  Second, if you're writing code that can run on transactional or non-transactional databases, some non-transactional databases will throw errors if they receive a "commit" or "rollback" query, but by calling the commit() and rollback() methods you instruct the database connection daemon to call the commit and rollback API methods for that database rather than issuing them as queries.  If the API's have no commit or rollback methods, the calls do nothing and the database throws no error.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#0000FF">using</font></b> System<font color="#990000">;</font>
<b><font color="#0000FF">using</font></b> SQLRClient<font color="#990000">;</font>
<b><font color="#0000FF">using</font></b> System<font color="#990000">.</font>Data<font color="#990000">;</font>
<b><font color="#0000FF">using</font></b> System<font color="#990000">.</font>IO<font color="#990000">;</font>

<b><font color="#0000FF">namespace</font></b> SQLRExamples
<font color="#FF0000">{</font>
	<b><font color="#0000FF">class</font></b> <font color="#008080">SQLRExample</font>
	<font color="#FF0000">{</font>
		<b><font color="#0000FF">public</font></b> <b><font color="#0000FF">static</font></b> <font color="#009900">void</font> <b><font color="#000000">Main</font></b><font color="#990000">()</font>
		<font color="#FF0000">{</font>
			<font color="#008080">SQLRelayConnection</font> sqlrcon <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">SQLRelayConnection</font></b><font color="#990000">(</font><font color="#FF0000">"Data Source=sqlrserver:9000;User ID=test;Password=test;Retry Time=0;Tries=1;Debug=false"</font><font color="#990000">);</font>
			sqlrcon<font color="#990000">.</font><b><font color="#000000">Open</font></b><font color="#990000">();</font>

			<font color="#008080">SQLRelayTransaction</font> sqlrtran <font color="#990000">=</font> sqlrcon<font color="#990000">.</font><b><font color="#000000">BeginTransaction</font></b><font color="#990000">();</font>

			<font color="#990000">...</font> run some queries that we want to keep the <font color="#008080">results</font> of <font color="#990000">...</font>

			sqlrtran<font color="#990000">.</font><b><font color="#000000">Commit</font></b><font color="#990000">();</font>

			<font color="#990000">...</font> run some queries that <font color="#008080">we</font> don<font color="#FF0000">'t want to keep the results of ...</font>

<font color="#FF0000">			sqlrtran.Rollback();</font>
<font color="#FF0000">		}</font>
<font color="#FF0000">	}</font>
<font color="#FF0000">}</font>
</tt></pre>

</blockquote>
<a name="temptables"/><h2>Temporary Tables</h2>

<p>Some databases support temporary tables.  That is, tables which are
automatically dropped or truncated when an application closes its connection
to the database or when a transaction is committed or rolled back.</p>

<p>For databases which drop or truncate tables when a transaction is committed
or rolled back, temporary tables work naturally.</p>

<p>However, for databases which drop or truncate tables when an application
closes its connection to the database, there is an issue.  Since SQL Relay
maintains persistent database connections, when an application disconnects from
SQL Relay, the connection between SQL Relay and the database remains, so
the database does not know to drop or truncate the table.  To remedy this
situation, SQL Relay parses each query to see if it created a temporary table,
keeps a list of temporary tables and drops (or truncates them) when the
application disconnects from SQL Relay.  Since each database has slightly
different syntax for creating a temporary table, SQL Relay parses each query
according to the rules for that database.</p>

<p>In effect, temporary tables should work when an application connects to
SQL Relay in the same manner that they would work if the application connected
directly to the database.</p>

<a name="errors"/><h2>Catching Errors</h2>

<p>If your calls to ExecuteNonQuery, ExecuteScalar or ExecuteReader fail, you can catch the error an exception.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#0000FF">using</font></b> System<font color="#990000">;</font>
<b><font color="#0000FF">using</font></b> SQLRClient<font color="#990000">;</font>
<b><font color="#0000FF">using</font></b> System<font color="#990000">.</font>Data<font color="#990000">;</font>
<b><font color="#0000FF">using</font></b> System<font color="#990000">.</font>IO<font color="#990000">;</font>

<b><font color="#0000FF">namespace</font></b> SQLRExamples
<font color="#FF0000">{</font>
	<b><font color="#0000FF">class</font></b> <font color="#008080">SQLRExample</font>
	<font color="#FF0000">{</font>
		<b><font color="#0000FF">public</font></b> <b><font color="#0000FF">static</font></b> <font color="#009900">void</font> <b><font color="#000000">Main</font></b><font color="#990000">()</font>
		<font color="#FF0000">{</font>
			<font color="#008080">SQLRelayConnection</font> sqlrcon <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">SQLRelayConnection</font></b><font color="#990000">(</font><font color="#FF0000">"Data Source=sqlrserver:9000;User ID=test;Password=test;Retry Time=0;Tries=1;Debug=false"</font><font color="#990000">);</font>
			sqlrcon<font color="#990000">.</font><b><font color="#000000">Open</font></b><font color="#990000">();</font>

			<font color="#008080">SQLRelayCommand</font> sqlrcom <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayComand<font color="#990000">)</font>sqlrcon<font color="#990000">.</font><b><font color="#000000">CreateCommand</font></b><font color="#990000">();</font>
			sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"insert into testtable values (1,'hello')"</font><font color="#990000">;</font>
			<b><font color="#0000FF">try</font></b>
			<font color="#FF0000">{</font>
				sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteNonQuery</font></b><font color="#990000">();</font>
			<font color="#FF0000">}</font>
			<b><font color="#0000FF">catch</font></b> <font color="#990000">(</font><font color="#008080">Exception</font> ex<font color="#990000">)</font>
			<font color="#FF0000">{</font>
				Console<font color="#990000">.</font><b><font color="#000000">WriteLine</font></b><font color="#990000">(</font>ex<font color="#990000">.</font>Message<font color="#990000">);</font>
			<font color="#FF0000">}</font>
		<font color="#FF0000">}</font>
	<font color="#FF0000">}</font>
<font color="#FF0000">}</font>
</tt></pre>

</blockquote>
<a name="bindvars"/><h2>Bind Variables</h2>

<p>Programs rarely execute fixed queries.  More often than not, some part
of the query is dynamically generated.  The ADO.NET API provides means
for using bind variables (also known as parameters) in those queries.</p>

<p>For a detailed discussion of binds, see 
<a href="binds.html">this document</a>.</p>

<p>To use bind variables (parameters) with the ADO.NET API, you must add variable/value pairs to the parameter collection associated with each instance of SQLRelayCommand.  For numeric, string or date variables, this is straightforward, you can just use the Parameters.Add() method.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#0000FF">using</font></b> System<font color="#990000">;</font>
<b><font color="#0000FF">using</font></b> SQLRClient<font color="#990000">;</font>
<b><font color="#0000FF">using</font></b> System<font color="#990000">.</font>Data<font color="#990000">;</font>
<b><font color="#0000FF">using</font></b> System<font color="#990000">.</font>IO<font color="#990000">;</font>

<b><font color="#0000FF">namespace</font></b> SQLRExamples
<font color="#FF0000">{</font>
	<b><font color="#0000FF">class</font></b> <font color="#008080">SQLRExample</font>
	<font color="#FF0000">{</font>
		<b><font color="#0000FF">public</font></b> <b><font color="#0000FF">static</font></b> <font color="#009900">void</font> <b><font color="#000000">Main</font></b><font color="#990000">()</font>
		<font color="#FF0000">{</font>
			<font color="#008080">SQLRelayConnection</font> sqlrcon <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">SQLRelayConnection</font></b><font color="#990000">(</font><font color="#FF0000">"Data Source=sqlrserver:9000;User ID=test;Password=test;Retry Time=0;Tries=1;Debug=false"</font><font color="#990000">);</font>
			sqlrcon<font color="#990000">.</font><b><font color="#000000">Open</font></b><font color="#990000">();</font>

			<font color="#008080">SQLRelayCommand</font> sqlrcom <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayComand<font color="#990000">)</font>sqlrcon<font color="#990000">.</font><b><font color="#000000">CreateCommand</font></b><font color="#990000">();</font>
			sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"insert into testtable values (:var1,:var2,:var3)"</font><font color="#990000">;</font>
			sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"var1"</font><font color="#990000">,</font> <font color="#993399">1</font><font color="#990000">);</font>
			sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"var2"</font><font color="#990000">,</font> <font color="#FF0000">"hello"</font><font color="#990000">);</font>
			sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"var2"</font><font color="#990000">,</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">DateTime</font></b><font color="#990000">(</font><font color="#993399">2001</font><font color="#990000">,</font> <font color="#993399">1</font><font color="#990000">,</font> <font color="#993399">1</font><font color="#990000">,</font> <font color="#993399">0</font><font color="#990000">,</font> <font color="#993399">0</font><font color="#990000">,</font> <font color="#993399">0</font><font color="#990000">));</font>
			<b><font color="#0000FF">try</font></b>
			<font color="#FF0000">{</font>
				sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteNonQuery</font></b><font color="#990000">();</font>
			<font color="#FF0000">}</font>
			<b><font color="#0000FF">catch</font></b> <font color="#990000">(</font><font color="#008080">Exception</font> ex<font color="#990000">)</font>
			<font color="#FF0000">{</font>
				Console<font color="#990000">.</font><b><font color="#000000">WriteLine</font></b><font color="#990000">(</font>ex<font color="#990000">.</font>Message<font color="#990000">);</font>
			<font color="#FF0000">}</font>
		<font color="#FF0000">}</font>
	<font color="#FF0000">}</font>
<font color="#FF0000">}</font>
</tt></pre>

</blockquote>
<p>You can also bind CLOB's and BLOB's.  It is a little more complex but not terribly.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#0000FF">using</font></b> System<font color="#990000">;</font>
<b><font color="#0000FF">using</font></b> SQLRClient<font color="#990000">;</font>
<b><font color="#0000FF">using</font></b> System<font color="#990000">.</font>Data<font color="#990000">;</font>
<b><font color="#0000FF">using</font></b> System<font color="#990000">.</font>IO<font color="#990000">;</font>

<b><font color="#0000FF">namespace</font></b> SQLRExamples
<font color="#FF0000">{</font>
	<b><font color="#0000FF">class</font></b> <font color="#008080">SQLRExample</font>
	<font color="#FF0000">{</font>
		<b><font color="#0000FF">public</font></b> <b><font color="#0000FF">static</font></b> <font color="#009900">void</font> <b><font color="#000000">Main</font></b><font color="#990000">()</font>
		<font color="#FF0000">{</font>
			<font color="#008080">SQLRelayConnection</font> sqlrcon <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">SQLRelayConnection</font></b><font color="#990000">(</font><font color="#FF0000">"Data Source=sqlrserver:9000;User ID=test;Password=test;Retry Time=0;Tries=1;Debug=false"</font><font color="#990000">);</font>
			sqlrcon<font color="#990000">.</font><b><font color="#000000">Open</font></b><font color="#990000">();</font>

			<font color="#008080">SQLRelayCommand</font> sqlrcom <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayComand<font color="#990000">)</font>sqlrcon<font color="#990000">.</font><b><font color="#000000">CreateCommand</font></b><font color="#990000">();</font>
			sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"insert into testtable values (:clobvar,:blobvar)"</font><font color="#990000">;</font>

			<font color="#008080">SQLRelayParameter</font> clobvar <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">SQLRelayParameter</font></b><font color="#990000">();</font>
			clobvar<font color="#990000">.</font>ParameterName <font color="#990000">=</font> <font color="#FF0000">"clobvar"</font><font color="#990000">;</font>
			clobvar<font color="#990000">.</font>Value <font color="#990000">=</font> <font color="#FF0000">"testclob"</font><font color="#990000">;</font>
			clobvar<font color="#990000">.</font>SQLRelayType <font color="#990000">=</font> SQLRelayType<font color="#990000">.</font>Clob<font color="#990000">;</font>
			sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font>clobvar<font color="#990000">);</font>

			<font color="#008080">SQLRelayParameter</font> blobvar <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">SQLRelayParameter</font></b><font color="#990000">();</font>
			blobvar<font color="#990000">.</font>ParameterName <font color="#990000">=</font> <font color="#FF0000">"blobvar"</font><font color="#990000">;</font>
			blobvar<font color="#990000">.</font>Value <font color="#990000">=</font> System<font color="#990000">.</font>Text<font color="#990000">.</font>Encoding<font color="#990000">.</font>Default<font color="#990000">.</font><b><font color="#000000">GetBytes</font></b><font color="#990000">(</font><font color="#FF0000">"testblob"</font><font color="#990000">);</font>
			blobvar<font color="#990000">.</font>SQLRelayType <font color="#990000">=</font> SQLRelayType<font color="#990000">.</font>Blob<font color="#990000">;</font>
			sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font>blobvar<font color="#990000">);</font>

			<b><font color="#0000FF">try</font></b>
			<font color="#FF0000">{</font>
				sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteNonQuery</font></b><font color="#990000">();</font>
			<font color="#FF0000">}</font>
			<b><font color="#0000FF">catch</font></b> <font color="#990000">(</font><font color="#008080">Exception</font> ex<font color="#990000">)</font>
			<font color="#FF0000">{</font>
				Console<font color="#990000">.</font><b><font color="#000000">WriteLine</font></b><font color="#990000">(</font>ex<font color="#990000">.</font>Message<font color="#990000">);</font>
			<font color="#FF0000">}</font>
		<font color="#FF0000">}</font>
	<font color="#FF0000">}</font>
<font color="#FF0000">}</font>
</tt></pre>

</blockquote>
<p>Note that in the above code, the Parameter.SQLRelayType must be set rather than Parameter.DbType, which would generally be set.  DbType is constrained to a set of values defined in the ADO.NET spec, which does not include Clob or Blob types.</p>

<p>Output bind variables can be used too.  The Direction and DbType attributes of the parameter must be specified though and for strings, the Size of the buffer to allocate for the return value must be specified as well.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#0000FF">using</font></b> System<font color="#990000">;</font>
<b><font color="#0000FF">using</font></b> SQLRClient<font color="#990000">;</font>
<b><font color="#0000FF">using</font></b> System<font color="#990000">.</font>Data<font color="#990000">;</font>
<b><font color="#0000FF">using</font></b> System<font color="#990000">.</font>IO<font color="#990000">;</font>

<b><font color="#0000FF">namespace</font></b> SQLRExamples
<font color="#FF0000">{</font>
	<b><font color="#0000FF">class</font></b> <font color="#008080">SQLRExample</font>
	<font color="#FF0000">{</font>
		<b><font color="#0000FF">public</font></b> <b><font color="#0000FF">static</font></b> <font color="#009900">void</font> <b><font color="#000000">Main</font></b><font color="#990000">()</font>
		<font color="#FF0000">{</font>
			<font color="#008080">SQLRelayConnection</font> sqlrcon <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">SQLRelayConnection</font></b><font color="#990000">(</font><font color="#FF0000">"Data Source=sqlrserver:9000;User ID=test;Password=test;Retry Time=0;Tries=1;Debug=false"</font><font color="#990000">);</font>
			sqlrcon<font color="#990000">.</font><b><font color="#000000">Open</font></b><font color="#990000">();</font>

			<font color="#008080">SQLRelayCommand</font> sqlrcom <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayComand<font color="#990000">)</font>sqlrcon<font color="#990000">.</font><b><font color="#000000">CreateCommand</font></b><font color="#990000">();</font>
			sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"begin  :numvar:=1;  :stringvar:='hello';  :floatvar:=2.5;  :datevar:='03-FEB-2001'; end;"</font><font color="#990000">;</font>

			<font color="#008080">SQLRelayParameter</font> numvar <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">SQLRelayParameter</font></b><font color="#990000">();</font>
			numvar<font color="#990000">.</font>ParameterName <font color="#990000">=</font> <font color="#FF0000">"numvar"</font><font color="#990000">;</font>
			numvar<font color="#990000">.</font>Direction <font color="#990000">=</font> ParameterDirection<font color="#990000">.</font>Output<font color="#990000">;</font>
			numvar<font color="#990000">.</font>DbType <font color="#990000">=</font> DbType<font color="#990000">.</font>Int64<font color="#990000">;</font>
			sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font>numvar<font color="#990000">);</font>

			<font color="#008080">SQLRelayParameter</font> stringvar <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">SQLRelayParameter</font></b><font color="#990000">();</font>
			stringvar<font color="#990000">.</font>ParameterName <font color="#990000">=</font> <font color="#FF0000">"stringvar"</font><font color="#990000">;</font>
			stringvar<font color="#990000">.</font>Direction <font color="#990000">=</font> ParameterDirection<font color="#990000">.</font>Output<font color="#990000">;</font>
			stringvar<font color="#990000">.</font>DbType <font color="#990000">=</font> DbType<font color="#990000">.</font>String<font color="#990000">;</font>
			stringvar<font color="#990000">.</font>Size <font color="#990000">=</font> <font color="#993399">20</font><font color="#990000">;</font>
			sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font>stringvar<font color="#990000">);</font>

			<font color="#008080">SQLRelayParameter</font> floatvar <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">SQLRelayParameter</font></b><font color="#990000">();</font>
			floatvar<font color="#990000">.</font>ParameterName <font color="#990000">=</font> <font color="#FF0000">"floatvar"</font><font color="#990000">;</font>
			floatvar<font color="#990000">.</font>Direction <font color="#990000">=</font> ParameterDirection<font color="#990000">.</font>Output<font color="#990000">;</font>
			floatvar<font color="#990000">.</font>DbType <font color="#990000">=</font> DbType<font color="#990000">.</font>Double<font color="#990000">;</font>
			sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font>floatvar<font color="#990000">);</font>

			<font color="#008080">SQLRelayParameter</font> datevar <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">SQLRelayParameter</font></b><font color="#990000">();</font>
			datevar<font color="#990000">.</font>ParameterName <font color="#990000">=</font> <font color="#FF0000">"datevar"</font><font color="#990000">;</font>
			datevar<font color="#990000">.</font>Direction <font color="#990000">=</font> ParameterDirection<font color="#990000">.</font>Output<font color="#990000">;</font>
			datevar<font color="#990000">.</font>DbType <font color="#990000">=</font> DbType<font color="#990000">.</font>DateTime<font color="#990000">;</font>
			sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font>datevar<font color="#990000">);</font>

			<b><font color="#0000FF">try</font></b>
			<font color="#FF0000">{</font>
				sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteNonQuery</font></b><font color="#990000">();</font>
			<font color="#FF0000">}</font>
			<b><font color="#0000FF">catch</font></b> <font color="#990000">(</font><font color="#008080">Exception</font> ex<font color="#990000">)</font>
			<font color="#FF0000">{</font>
				Console<font color="#990000">.</font><b><font color="#000000">WriteLine</font></b><font color="#990000">(</font>ex<font color="#990000">.</font>Message<font color="#990000">);</font>
			<font color="#FF0000">}</font>

			<font color="#990000">...</font> <b><font color="#0000FF">do</font></b> something <font color="#008080">with</font> numvar<font color="#990000">.</font>Value <font color="#990000">...</font>
			<font color="#990000">...</font> <b><font color="#0000FF">do</font></b> something <font color="#008080">with</font> stringvar<font color="#990000">.</font>Value <font color="#990000">...</font>
			<font color="#990000">...</font> <b><font color="#0000FF">do</font></b> something <font color="#008080">with</font> floatvar<font color="#990000">.</font>Value <font color="#990000">...</font>
			<font color="#990000">...</font> <b><font color="#0000FF">do</font></b> something <font color="#008080">with</font> datevar<font color="#990000">.</font>Value <font color="#990000">...</font>
		<font color="#FF0000">}</font>
	<font color="#FF0000">}</font>
<font color="#FF0000">}</font>
</tt></pre>

</blockquote>
<p>FIXME: ... clob, blob and cursor output bind ...</p>

<a name="rebinding"/><h2>Re-Binding and Re-Execution</h2>

<p>A feature of the prepare/bind/execute paradigm is the ability to prepare, 
bind and execute a query once, then re-bind and re-execute the query 
over and over without re-preparing it.  If your backend database natively 
supports this paradigm, you can reap a substantial performance improvement.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#0000FF">using</font></b> System<font color="#990000">;</font>
<b><font color="#0000FF">using</font></b> SQLRClient<font color="#990000">;</font>
<b><font color="#0000FF">using</font></b> System<font color="#990000">.</font>Data<font color="#990000">;</font>
<b><font color="#0000FF">using</font></b> System<font color="#990000">.</font>IO<font color="#990000">;</font>

<b><font color="#0000FF">namespace</font></b> SQLRExamples
<font color="#FF0000">{</font>
	<b><font color="#0000FF">class</font></b> <font color="#008080">SQLRExample</font>
	<font color="#FF0000">{</font>
		<b><font color="#0000FF">public</font></b> <b><font color="#0000FF">static</font></b> <font color="#009900">void</font> <b><font color="#000000">Main</font></b><font color="#990000">()</font>
		<font color="#FF0000">{</font>
			<font color="#008080">SQLRelayConnection</font> sqlrcon <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">SQLRelayConnection</font></b><font color="#990000">(</font><font color="#FF0000">"Data Source=sqlrserver:9000;User ID=test;Password=test;Retry Time=0;Tries=1;Debug=false"</font><font color="#990000">);</font>
			sqlrcon<font color="#990000">.</font><b><font color="#000000">Open</font></b><font color="#990000">();</font>

			<i><font color="#9A1900">// Prepare the query...</font></i>
			<font color="#008080">SQLRelayCommand</font> sqlrcom <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayComand<font color="#990000">)</font>sqlrcon<font color="#990000">.</font><b><font color="#000000">CreateCommand</font></b><font color="#990000">();</font>
			sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"insert into testtable values (:var1,:var2,:var3)"</font><font color="#990000">;</font>
			sqlrcom<font color="#990000">.</font><b><font color="#000000">Prepare</font></b><font color="#990000">();</font>


			<i><font color="#9A1900">// Execute once...</font></i>
			sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"var1"</font><font color="#990000">,</font> <font color="#993399">1</font><font color="#990000">);</font>
			sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"var2"</font><font color="#990000">,</font> <font color="#FF0000">"hello"</font><font color="#990000">);</font>
			sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"var2"</font><font color="#990000">,</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">DateTime</font></b><font color="#990000">(</font><font color="#993399">2001</font><font color="#990000">,</font> <font color="#993399">1</font><font color="#990000">,</font> <font color="#993399">1</font><font color="#990000">,</font> <font color="#993399">0</font><font color="#990000">,</font> <font color="#993399">0</font><font color="#990000">,</font> <font color="#993399">0</font><font color="#990000">));</font>
			<b><font color="#0000FF">try</font></b>
			<font color="#FF0000">{</font>
				sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteNonQuery</font></b><font color="#990000">();</font>
			<font color="#FF0000">}</font>
			<b><font color="#0000FF">catch</font></b> <font color="#990000">(</font><font color="#008080">Exception</font> ex<font color="#990000">)</font>
			<font color="#FF0000">{</font>
				Console<font color="#990000">.</font><b><font color="#000000">WriteLine</font></b><font color="#990000">(</font>ex<font color="#990000">.</font>Message<font color="#990000">);</font>
			<font color="#FF0000">}</font>
			sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Clear</font></b><font color="#990000">();</font>


			<i><font color="#9A1900">// Execute again with new values...</font></i>
			sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"var1"</font><font color="#990000">,</font> <font color="#993399">2</font><font color="#990000">);</font>
			sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"var2"</font><font color="#990000">,</font> <font color="#FF0000">"bye"</font><font color="#990000">);</font>
			sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"var2"</font><font color="#990000">,</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">DateTime</font></b><font color="#990000">(</font><font color="#993399">2002</font><font color="#990000">,</font> <font color="#993399">2</font><font color="#990000">,</font> <font color="#993399">2</font><font color="#990000">,</font> <font color="#993399">0</font><font color="#990000">,</font> <font color="#993399">0</font><font color="#990000">,</font> <font color="#993399">0</font><font color="#990000">));</font>
			<b><font color="#0000FF">try</font></b>
			<font color="#FF0000">{</font>
				sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteNonQuery</font></b><font color="#990000">();</font>
			<font color="#FF0000">}</font>
			<b><font color="#0000FF">catch</font></b> <font color="#990000">(</font><font color="#008080">Exception</font> ex<font color="#990000">)</font>
			<font color="#FF0000">{</font>
				Console<font color="#990000">.</font><b><font color="#000000">WriteLine</font></b><font color="#990000">(</font>ex<font color="#990000">.</font>Message<font color="#990000">);</font>
			<font color="#FF0000">}</font>


			<font color="#990000">...</font> re<font color="#990000">-</font>bind <font color="#008080">and</font> re<font color="#990000">-</font>execute again <font color="#008080">and</font> again <font color="#990000">...</font>
		<font color="#FF0000">}</font>
	<font color="#FF0000">}</font>
<font color="#FF0000">}</font>
</tt></pre>

</blockquote>
<a name="fields"/><h2>Accessing Fields in the Result Set</h2>

<p>Once returned from an ExecuteReader call, the SQLRelayDataReader provides methods for accessing the fields of the result set.</p>

<p>To fetch a row, call the Read() method.  This method will return false if no more rows are available.</p>

<p>After the row has been fetched, individual fields of that row may be accessed through a wide variety of methods.  The raw data for each field can be accessed by index using GetValue(), as a numeric array returned by the GetValues() or by using the SQLRelayDataReader directly as either a numeric or associative array.  The GetOrdinal() method can also be used to get the numeric index of a column name as well.</p>

<p>Further, the raw data for each field can be coaxed into a native type using the GetBoolean(), GetByte(), GetBytes(), GetChar(), GetChars(), GetGuid(), GetInt16(), GetInt32(), GetInt64(), GetFloat(), GetDouble(), GetString(), GetDecimal() and GetDateTime() methods.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#0000FF">using</font></b> System<font color="#990000">;</font>
<b><font color="#0000FF">using</font></b> SQLRClient<font color="#990000">;</font>
<b><font color="#0000FF">using</font></b> System<font color="#990000">.</font>Data<font color="#990000">;</font>
<b><font color="#0000FF">using</font></b> System<font color="#990000">.</font>IO<font color="#990000">;</font>

<b><font color="#0000FF">namespace</font></b> SQLRExamples
<font color="#FF0000">{</font>
	<b><font color="#0000FF">class</font></b> <font color="#008080">SQLRExample</font>
	<font color="#FF0000">{</font>
		<b><font color="#0000FF">public</font></b> <b><font color="#0000FF">static</font></b> <font color="#009900">void</font> <b><font color="#000000">Main</font></b><font color="#990000">()</font>
		<font color="#FF0000">{</font>
			<font color="#008080">SQLRelayConnection</font> sqlrcon <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">SQLRelayConnection</font></b><font color="#990000">(</font><font color="#FF0000">"Data Source=sqlrserver:9000;User ID=test;Password=test;Retry Time=0;Tries=1;Debug=false"</font><font color="#990000">);</font>
			sqlrcon<font color="#990000">.</font><b><font color="#000000">Open</font></b><font color="#990000">();</font>

			<font color="#008080">SQLRelayCommand</font> sqlrcom <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayComand<font color="#990000">)</font>sqlrcon<font color="#990000">.</font><b><font color="#000000">CreateCommand</font></b><font color="#990000">();</font>
			sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"select col1,col2 from testtable"</font><font color="#990000">;</font>

			<b><font color="#0000FF">try</font></b>
			<font color="#FF0000">{</font>
				System<font color="#990000">.</font>Data<font color="#990000">.</font><font color="#008080">IDataReader</font> datareader <font color="#990000">=</font> sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteReader</font></b><font color="#990000">();</font>
				
				<i><font color="#9A1900">// read row...</font></i>
				datareader<font color="#990000">.</font><b><font color="#000000">Read</font></b><font color="#990000">();</font>

				<i><font color="#9A1900">// get the raw data of the first field in a variety of ways</font></i>
				<font color="#008080">Object</font> o1 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetValue</font></b><font color="#990000">(</font><font color="#993399">0</font><font color="#990000">);</font>
				o1 <font color="#990000">=</font> datareader<font color="#990000">[</font><font color="#993399">0</font><font color="#990000">];</font>
				o1 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetValue</font></b><font color="#990000">(</font>datareader<font color="#990000">.</font><b><font color="#000000">GetOrdinal</font></b><font color="#990000">(</font><font color="#FF0000">"col1"</font><font color="#990000">));</font>
				o1 <font color="#990000">=</font> datareader<font color="#990000">[</font><font color="#FF0000">"col1"</font><font color="#990000">];</font>
				Object<font color="#990000">[]</font> os1 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetValues</font></b><font color="#990000">();</font>
				o1 <font color="#990000">=</font> os1<font color="#990000">[</font><font color="#993399">0</font><font color="#990000">];</font>

				<i><font color="#9A1900">// get the first field as a string</font></i>
				<font color="#008080">String</font> s1 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetString</font></b><font color="#990000">(</font><font color="#993399">0</font><font color="#990000">);</font>


				<i><font color="#9A1900">// get the raw data of the second field in a variety of ways</font></i>
				<font color="#008080">Object</font> o2 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetValue</font></b><font color="#990000">(</font><font color="#993399">1</font><font color="#990000">);</font>
				o2 <font color="#990000">=</font> datareader<font color="#990000">[</font><font color="#993399">1</font><font color="#990000">];</font>
				o2 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetValue</font></b><font color="#990000">(</font>datareader<font color="#990000">.</font><b><font color="#000000">GetOrdinal</font></b><font color="#990000">(</font><font color="#FF0000">"col2"</font><font color="#990000">));</font>
				o2 <font color="#990000">=</font> datareader<font color="#990000">[</font><font color="#FF0000">"col2"</font><font color="#990000">];</font>
				Object<font color="#990000">[]</font> os2 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetValues</font></b><font color="#990000">();</font>
				o2 <font color="#990000">=</font> os2<font color="#990000">[</font><font color="#993399">1</font><font color="#990000">];</font>

				<i><font color="#9A1900">// get the second field as a string</font></i>
				<font color="#008080">String</font> s2 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetString</font></b><font color="#990000">(</font><font color="#993399">1</font><font color="#990000">);</font>
				
				<i><font color="#9A1900">// read another row...</font></i>
				datareader<font color="#990000">.</font><b><font color="#000000">Read</font></b><font color="#990000">();</font>

				<font color="#990000">...</font> <b><font color="#0000FF">do</font></b> something with <font color="#008080">this</font> row <font color="#990000">...</font>

				<font color="#990000">...</font> fetch <font color="#008080">more</font> rows <font color="#990000">...</font>
			<font color="#FF0000">}</font>
			<b><font color="#0000FF">catch</font></b> <font color="#990000">(</font><font color="#008080">Exception</font> ex<font color="#990000">)</font>
			<font color="#FF0000">{</font>
				Console<font color="#990000">.</font><b><font color="#000000">WriteLine</font></b><font color="#990000">(</font>ex<font color="#990000">.</font>Message<font color="#990000">);</font>
			<font color="#FF0000">}</font>
		<font color="#FF0000">}</font>
	<font color="#FF0000">}</font>
<font color="#FF0000">}</font>
</tt></pre>

</blockquote>
<br/><a name="largeresultsets"/><h2>Dealing With Large Result Sets</h2>

<p>SQL Relay normally buffers the entire result set.  This can speed things up 
at the cost of memory.  With large enough result sets, it makes sense to buffer the result set in chunks instead of all at once.</p>

<p>Use the parameter SQLRelayCommand.ResultSetBufferSize to set the number of rows to buffer at a time.  Calls to SQLRelayDataReader.Read() cause the chunk containing the requested field to be fetched.</p>

<a name="cursors"/><h2>Cursors</h2>

<p>Cursors make it possible to execute queries while processing the result
set of another query.  You can select rows from a table in one query, then 
iterate through its result set, inserting rows into another table, using only 
1 database connection for both operations.</p>

<p>In the ADO.NET API, cursors are abstracted by the SQLRelayCommand class.  Each SQLRelayCommand can be thought of as a separate cursor and in many cases, each SQLRelayCommand consumes a separate server-side cursor.</p>

<p>For example:</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#0000FF">using</font></b> System<font color="#990000">;</font>
<b><font color="#0000FF">using</font></b> SQLRClient<font color="#990000">;</font>
<b><font color="#0000FF">using</font></b> System<font color="#990000">.</font>Data<font color="#990000">;</font>
<b><font color="#0000FF">using</font></b> System<font color="#990000">.</font>IO<font color="#990000">;</font>

<b><font color="#0000FF">namespace</font></b> SQLRExamples
<font color="#FF0000">{</font>
	<b><font color="#0000FF">class</font></b> <font color="#008080">SQLRExample</font>
	<font color="#FF0000">{</font>
		<b><font color="#0000FF">public</font></b> <b><font color="#0000FF">static</font></b> <font color="#009900">void</font> <b><font color="#000000">Main</font></b><font color="#990000">()</font>
		<font color="#FF0000">{</font>
			<font color="#008080">SQLRelayConnection</font> sqlrcon <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">SQLRelayConnection</font></b><font color="#990000">(</font><font color="#FF0000">"Data Source=sqlrserver:9000;User ID=test;Password=test;Retry Time=0;Tries=1;Debug=false"</font><font color="#990000">);</font>
			sqlrcon<font color="#990000">.</font><b><font color="#000000">Open</font></b><font color="#990000">();</font>

			<font color="#008080">SQLRelayCommand</font> sqlrcom1 <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayComand<font color="#990000">)</font>sqlrcon<font color="#990000">.</font><b><font color="#000000">CreateCommand</font></b><font color="#990000">();</font>
			sqlrcom1<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"select * from my_first_table"</font><font color="#990000">;</font>


			<font color="#008080">SQLRelayCommand</font> sqlrcom2 <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayComand<font color="#990000">)</font>sqlrcon<font color="#990000">.</font><b><font color="#000000">CreateCommand</font></b><font color="#990000">();</font>
			sqlrcom2<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"insert into my_second_table values (:col1, :col2, :col3)"</font><font color="#990000">;</font>

			<b><font color="#0000FF">try</font></b>
			<font color="#FF0000">{</font>
				System<font color="#990000">.</font>Data<font color="#990000">.</font><font color="#008080">IDataReader</font> datareader <font color="#990000">=</font> sqlrcom1<font color="#990000">.</font><b><font color="#000000">ExecuteReader</font></b><font color="#990000">();</font>
				<b><font color="#0000FF">while</font></b> <font color="#990000">(</font>datareader<font color="#990000">.</font><b><font color="#000000">Read</font></b><font color="#990000">())</font>
				<font color="#FF0000">{</font>
					sqlrcom2<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Clear</font></b><font color="#990000">();</font>
					sqlrcom2<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"col1"</font><font color="#990000">,</font>datareader<font color="#990000">.</font><b><font color="#000000">GetString</font></b><font color="#990000">(</font><font color="#993399">0</font><font color="#990000">));</font>
					sqlrcom2<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"col2"</font><font color="#990000">,</font>datareader<font color="#990000">.</font><b><font color="#000000">GetString</font></b><font color="#990000">(</font><font color="#993399">1</font><font color="#990000">));</font>
					sqlrcom2<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"col3"</font><font color="#990000">,</font>datareader<font color="#990000">.</font><b><font color="#000000">GetString</font></b><font color="#990000">(</font><font color="#993399">2</font><font color="#990000">));</font>
					<b><font color="#0000FF">try</font></b>
					<font color="#FF0000">{</font>
						sqlrcom2<font color="#990000">.</font><b><font color="#000000">ExecuteNonQuery</font></b><font color="#990000">();</font>
					<font color="#FF0000">}</font>
					<b><font color="#0000FF">catch</font></b> <font color="#990000">(</font><font color="#008080">Exception</font> ex<font color="#990000">)</font>
					<font color="#FF0000">{</font>
						Console<font color="#990000">.</font><b><font color="#000000">WriteLine</font></b><font color="#990000">(</font>ex<font color="#990000">.</font>Message<font color="#990000">);</font>
					<font color="#FF0000">}</font>
				<font color="#FF0000">}</font>
				
			<font color="#FF0000">}</font>
			<b><font color="#0000FF">catch</font></b> <font color="#990000">(</font><font color="#008080">Exception</font> ex<font color="#990000">)</font>
			<font color="#FF0000">{</font>
				Console<font color="#990000">.</font><b><font color="#000000">WriteLine</font></b><font color="#990000">(</font>ex<font color="#990000">.</font>Message<font color="#990000">);</font>
			<font color="#FF0000">}</font>
		<font color="#FF0000">}</font>
	<font color="#FF0000">}</font>
<font color="#FF0000">}</font>
</tt></pre>

</blockquote>
<a name="columns"/><h2>Getting Column Information</h2>

<p>After executing a query, the column count is stored in the FieldCount property of the SQLRelayDataReader class.  Column names are accessible via the GetName() method.  Column types are available from the GetDataTypeName().  The native .NET data type for the field is avialable from the GetFieldType() method.  They may be used as follows:</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#0000FF">using</font></b> System<font color="#990000">;</font>
<b><font color="#0000FF">using</font></b> SQLRClient<font color="#990000">;</font>
<b><font color="#0000FF">using</font></b> System<font color="#990000">.</font>Data<font color="#990000">;</font>
<b><font color="#0000FF">using</font></b> System<font color="#990000">.</font>IO<font color="#990000">;</font>

<b><font color="#0000FF">namespace</font></b> SQLRExamples
<font color="#FF0000">{</font>
	<b><font color="#0000FF">class</font></b> <font color="#008080">SQLRExample</font>
	<font color="#FF0000">{</font>
		<b><font color="#0000FF">public</font></b> <b><font color="#0000FF">static</font></b> <font color="#009900">void</font> <b><font color="#000000">Main</font></b><font color="#990000">()</font>
		<font color="#FF0000">{</font>
			<font color="#008080">SQLRelayConnection</font> sqlrcon <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">SQLRelayConnection</font></b><font color="#990000">(</font><font color="#FF0000">"Data Source=sqlrserver:9000;User ID=test;Password=test;Retry Time=0;Tries=1;Debug=false"</font><font color="#990000">);</font>
			sqlrcon<font color="#990000">.</font><b><font color="#000000">Open</font></b><font color="#990000">();</font>

			<font color="#008080">SQLRelayCommand</font> sqlrcom <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayComand<font color="#990000">)</font>sqlrcon<font color="#990000">.</font><b><font color="#000000">CreateCommand</font></b><font color="#990000">();</font>
			sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"select * from my_first_table"</font><font color="#990000">;</font>

			<b><font color="#0000FF">try</font></b>
			<font color="#FF0000">{</font>
				System<font color="#990000">.</font>Data<font color="#990000">.</font><font color="#008080">IDataReader</font> datareader <font color="#990000">=</font> sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteReader</font></b><font color="#990000">();</font>
				<b><font color="#0000FF">for</font></b> <font color="#990000">(</font><font color="#008080">UInt32</font> index <font color="#990000">=</font> <font color="#993399">0</font><font color="#990000">;</font> <font color="#008080">index</font> <font color="#990000">&amp;</font>lt<font color="#990000">;</font> sqlrcom<font color="#990000">.</font>FieldCount<font color="#990000">;</font> index<font color="#990000">++)</font>
				<font color="#FF0000">{</font>
					<font color="#008080">String</font> name <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetName</font></b><font color="#990000">(</font>index<font color="#990000">);</font>
					<font color="#008080">String</font> datatype <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetDataTypename</font></b><font color="#990000">(</font>index<font color="#990000">);</font>
					<font color="#008080">String</font> nativedatatype <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetFieldType</font></b><font color="#990000">(</font>index<font color="#990000">).</font><b><font color="#000000">ToString</font></b><font color="#990000">();</font>

					<font color="#990000">...</font> <b><font color="#0000FF">do</font></b> someting with the <font color="#008080">column</font> data <font color="#990000">...</font>
				<font color="#FF0000">}</font>
			<font color="#FF0000">}</font>
			<b><font color="#0000FF">catch</font></b> <font color="#990000">(</font><font color="#008080">Exception</font> ex<font color="#990000">)</font>
			<font color="#FF0000">{</font>
				Console<font color="#990000">.</font><b><font color="#000000">WriteLine</font></b><font color="#990000">(</font>ex<font color="#990000">.</font>Message<font color="#990000">);</font>
			<font color="#FF0000">}</font>
		<font color="#FF0000">}</font>
	<font color="#FF0000">}</font>
<font color="#FF0000">}</font>
</tt></pre>

</blockquote>
<p>Extended column information is available from the GetSchemaTable method.  Usage is best illustrated by example.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><b><font color="#0000FF">using</font></b> System<font color="#990000">;</font>
<b><font color="#0000FF">using</font></b> SQLRClient<font color="#990000">;</font>
<b><font color="#0000FF">using</font></b> System<font color="#990000">.</font>Data<font color="#990000">;</font>
<b><font color="#0000FF">using</font></b> System<font color="#990000">.</font>IO<font color="#990000">;</font>

<b><font color="#0000FF">namespace</font></b> SQLRExamples
<font color="#FF0000">{</font>
	<b><font color="#0000FF">class</font></b> <font color="#008080">SQLRExample</font>
	<font color="#FF0000">{</font>
		<b><font color="#0000FF">public</font></b> <b><font color="#0000FF">static</font></b> <font color="#009900">void</font> <b><font color="#000000">Main</font></b><font color="#990000">()</font>
		<font color="#FF0000">{</font>
			<font color="#008080">SQLRelayConnection</font> sqlrcon <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">SQLRelayConnection</font></b><font color="#990000">(</font><font color="#FF0000">"Data Source=sqlrserver:9000;User ID=test;Password=test;Retry Time=0;Tries=1;Debug=false"</font><font color="#990000">);</font>
			sqlrcon<font color="#990000">.</font><b><font color="#000000">Open</font></b><font color="#990000">();</font>

			<font color="#008080">SQLRelayCommand</font> sqlrcom <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayComand<font color="#990000">)</font>sqlrcon<font color="#990000">.</font><b><font color="#000000">CreateCommand</font></b><font color="#990000">();</font>
			sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"select * from my_first_table"</font><font color="#990000">;</font>

			<b><font color="#0000FF">try</font></b>
			<font color="#FF0000">{</font>
				System<font color="#990000">.</font>Data<font color="#990000">.</font><font color="#008080">IDataReader</font> datareader <font color="#990000">=</font> sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteReader</font></b><font color="#990000">();</font>

				<font color="#008080">DataTable</font> schematable <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetSchemaTable</font></b><font color="#990000">();</font>
				<b><font color="#0000FF">for</font></b> <font color="#990000">(</font><font color="#008080">UInt32</font> index <font color="#990000">=</font> <font color="#993399">0</font><font color="#990000">;</font> <font color="#008080">index</font> <font color="#990000">&amp;</font>lt<font color="#990000">;</font> sqlrcom<font color="#990000">.</font>FieldCount<font color="#990000">;</font> index<font color="#990000">++)</font>
				<font color="#FF0000">{</font>
					<font color="#008080">String</font> columnname <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToString</font></b><font color="#990000">(</font>schematable<font color="#990000">.</font>Rows<font color="#990000">[</font>index<font color="#990000">][</font><font color="#FF0000">"ColumnName"</font><font color="#990000">]);</font>
					<font color="#008080">Int64</font> columnordinal <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToInt64</font></b><font color="#990000">(</font>schematable<font color="#990000">.</font>Rows<font color="#990000">[</font>index<font color="#990000">][</font><font color="#FF0000">"ColumnOrdinal"</font><font color="#990000">]);</font>
            				<font color="#008080">Int64</font> columnsize <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToInt64</font></b><font color="#990000">(</font>schematable<font color="#990000">.</font>Rows<font color="#990000">[</font>index<font color="#990000">][</font><font color="#FF0000">"ColumnSize"</font><font color="#990000">]);</font>
            				<font color="#008080">Int64</font> numericprecision <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToInt64</font></b><font color="#990000">(</font>schematable<font color="#990000">.</font>Rows<font color="#990000">[</font>index<font color="#990000">][</font><font color="#FF0000">"NumericPrecision"</font><font color="#990000">]);</font>
            				<font color="#008080">Int64</font> numericscale <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToInt64</font></b><font color="#990000">(</font>schematable<font color="#990000">.</font>Rows<font color="#990000">[</font>index<font color="#990000">][</font><font color="#FF0000">"NumericScale"</font><font color="#990000">]);</font>
            				<font color="#008080">Boolean</font> isunique <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToBoolean</font></b><font color="#990000">(</font>schematable<font color="#990000">.</font>Rows<font color="#990000">[</font>index<font color="#990000">][</font><font color="#FF0000">"IsUnique"</font><font color="#990000">]);</font>
            				<font color="#008080">Boolean</font> iskey <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToBoolean</font></b><font color="#990000">(</font>schematable<font color="#990000">.</font>Rows<font color="#990000">[</font>index<font color="#990000">][</font><font color="#FF0000">"IsKey"</font><font color="#990000">]);</font>
            				<font color="#008080">String</font> baseservername <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToString</font></b><font color="#990000">(</font>schematable<font color="#990000">.</font>Rows<font color="#990000">[</font>index<font color="#990000">][</font><font color="#FF0000">"BaseServerName"</font><font color="#990000">]);</font>
            				<font color="#008080">String</font> basecatalogname <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToString</font></b><font color="#990000">(</font>schematable<font color="#990000">.</font>Rows<font color="#990000">[</font>index<font color="#990000">][</font><font color="#FF0000">"BaseCatalogName"</font><font color="#990000">]);</font>
            				<font color="#008080">String</font> basecolumnname <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToString</font></b><font color="#990000">(</font>schematable<font color="#990000">.</font>Rows<font color="#990000">[</font>index<font color="#990000">][</font><font color="#FF0000">"BaseColumnName"</font><font color="#990000">]);</font>
            				<font color="#008080">String</font> baseschemaname <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToString</font></b><font color="#990000">(</font>schematable<font color="#990000">.</font>Rows<font color="#990000">[</font>index<font color="#990000">][</font><font color="#FF0000">"BaseSchemaName"</font><font color="#990000">]);</font>
            				<font color="#008080">String</font> basetablename <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToString</font></b><font color="#990000">(</font>schematable<font color="#990000">.</font>Rows<font color="#990000">[</font>index<font color="#990000">][</font><font color="#FF0000">"BaseTableName"</font><font color="#990000">]);</font>
            				<font color="#008080">String</font> datatype <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToString</font></b><font color="#990000">(</font>schematable<font color="#990000">.</font>Rows<font color="#990000">[</font>index<font color="#990000">][</font><font color="#FF0000">"DataType"</font><font color="#990000">]);</font>
            				<font color="#008080">Boolean</font> allowdbnull <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToBoolean</font></b><font color="#990000">(</font>schematable<font color="#990000">.</font>Rows<font color="#990000">[</font>index<font color="#990000">][</font><font color="#FF0000">"AllowDBNull"</font><font color="#990000">]);</font>
            				<font color="#008080">String</font> providertype <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToString</font></b><font color="#990000">(</font>schematable<font color="#990000">.</font>Rows<font color="#990000">[</font>index<font color="#990000">][</font><font color="#FF0000">"ProviderType"</font><font color="#990000">]);</font>
            				<font color="#008080">Boolean</font> isaliased <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToBoolean</font></b><font color="#990000">(</font>schematable<font color="#990000">.</font>Rows<font color="#990000">[</font>index<font color="#990000">][</font><font color="#FF0000">"IsAliased"</font><font color="#990000">]);</font>
            				<font color="#008080">Boolean</font> isexpression <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToBoolean</font></b><font color="#990000">(</font>schematable<font color="#990000">.</font>Rows<font color="#990000">[</font>index<font color="#990000">][</font><font color="#FF0000">"IsExpression"</font><font color="#990000">]);</font>
            				<font color="#008080">Boolean</font> isidentity <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToBoolean</font></b><font color="#990000">(</font>schematable<font color="#990000">.</font>Rows<font color="#990000">[</font>index<font color="#990000">][</font><font color="#FF0000">"IsIdentity"</font><font color="#990000">]);</font>
            				<font color="#008080">Boolean</font> isautoincrement <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToBoolean</font></b><font color="#990000">(</font>schematable<font color="#990000">.</font>Rows<font color="#990000">[</font>index<font color="#990000">][</font><font color="#FF0000">"IsAutoIncrement"</font><font color="#990000">]);</font>
            				<font color="#008080">Boolean</font> isrowversion <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToBoolean</font></b><font color="#990000">(</font>schematable<font color="#990000">.</font>Rows<font color="#990000">[</font>index<font color="#990000">][</font><font color="#FF0000">"IsRowVersion"</font><font color="#990000">]);</font>
            				<font color="#008080">Boolean</font> ishidden <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToBoolean</font></b><font color="#990000">(</font>schematable<font color="#990000">.</font>Rows<font color="#990000">[</font>index<font color="#990000">][</font><font color="#FF0000">"IsHidden"</font><font color="#990000">]);</font>
            				<font color="#008080">Boolean</font> islong <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToBoolean</font></b><font color="#990000">(</font>schematable<font color="#990000">.</font>Rows<font color="#990000">[</font>index<font color="#990000">][</font><font color="#FF0000">"IsLong"</font><font color="#990000">]);</font>
            				<font color="#008080">Boolean</font> isreadonly <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToBoolean</font></b><font color="#990000">(</font>schematable<font color="#990000">.</font>Rows<font color="#990000">[</font>index<font color="#990000">][</font><font color="#FF0000">"IsReadOnly"</font><font color="#990000">]);</font>
            				<font color="#008080">String</font> providerspecificdatatype <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToString</font></b><font color="#990000">(</font>schematable<font color="#990000">.</font>Rows<font color="#990000">[</font>index<font color="#990000">][</font><font color="#FF0000">"ProviderSpecificDataType"</font><font color="#990000">]);</font>
            				<font color="#008080">String</font> datatypename <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToString</font></b><font color="#990000">(</font>schematable<font color="#990000">.</font>Rows<font color="#990000">[</font>index<font color="#990000">][</font><font color="#FF0000">"DataTypeName"</font><font color="#990000">]);</font>
            				<font color="#008080">String</font> xmlschemacollectiondatabase <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToString</font></b><font color="#990000">(</font>schematable<font color="#990000">.</font>Rows<font color="#990000">[</font>index<font color="#990000">][</font><font color="#FF0000">"XmlSchemaCollectionDatabase"</font><font color="#990000">]);</font>
            				<font color="#008080">String</font> xmlschemacollectionowningschema <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToString</font></b><font color="#990000">(</font>schematable<font color="#990000">.</font>Rows<font color="#990000">[</font>index<font color="#990000">][</font><font color="#FF0000">"XmlSchemaCollectionOwningSchema"</font><font color="#990000">]);</font>
            				<font color="#008080">String</font> xmlschemacollectionname <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToString</font></b><font color="#990000">(</font>schematable<font color="#990000">.</font>Rows<font color="#990000">[</font>index<font color="#990000">][</font><font color="#FF0000">"XmlSchemaCollectionName"</font><font color="#990000">]);</font>

					<font color="#990000">...</font> <b><font color="#0000FF">do</font></b> something with all these bits <font color="#008080">of</font> information <font color="#990000">...</font>
				<font color="#FF0000">}</font>
			<font color="#FF0000">}</font>
			<b><font color="#0000FF">catch</font></b> <font color="#990000">(</font><font color="#008080">Exception</font> ex<font color="#990000">)</font>
			<font color="#FF0000">{</font>
				Console<font color="#990000">.</font><b><font color="#000000">WriteLine</font></b><font color="#990000">(</font>ex<font color="#990000">.</font>Message<font color="#990000">);</font>
			<font color="#FF0000">}</font>
		<font color="#FF0000">}</font>
	<font color="#FF0000">}</font>
<font color="#FF0000">}</font>
</tt></pre>

</blockquote>
<p>SQL Relay support most of the fields that might be returned in the schema table but not all.  Most databases don't support all of these fields either.  In the future, support may improve.</p>

<a name="storedprocedures"/><h2>Stored Procedures</h2>

<p>Many databases support stored procedures.  Stored procedures are sets of
queries and procedural code that are executed inside of the database itself.
For example, a stored procedure may select rows from one table, iterate through
the result set and, based on the values in each row, insert, update or delete
rows in other tables.  A client program could do this as well, but a stored
procedure is generally more efficient because queries and result sets don't
have to be sent back and forth between the client and database.  Also, stored
procedures are generally stored in the database in a compiled state, while
queries may have to be re-parsed and re-compiled each time they are sent.</p>

<p>While many databases support stored procedures.  The syntax for creating
and executing stored procedures varies greatly between databases.</p>

<p>Stored procedures typically take input paramters from client programs through
input bind variables and return values back to client programs either through
bind variables or result sets.  Stored procedures can be broken down into
several categories, based on the values that they return.  Some stored
procedures don't return any values, some return a single value, some return
multiple values and some return entire result sets.</p>

<h3>No Values</h3>

<p>Some stored procedures don't return any values.  Below are examples,
illustrating how to create, execute and drop this kind of stored procedure for
each database that SQL Relay supports.</p>

<h4>Oracle</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">testproc</font></b><font color="#990000">(</font>in1 <font color="#008080">in</font> number<font color="#990000">,</font> in2 <font color="#008080">in</font> number<font color="#990000">,</font> in3 <font color="#008080">in</font> varchar2<font color="#990000">)</font> is
begin
        insert into <font color="#008080">mytable</font> <b><font color="#000000">values</font></b> <font color="#990000">(</font>in1<font color="#990000">,</font>in2<font color="#990000">,</font>in3<font color="#990000">);</font>
end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#008080">SQLRelayCommand</font> sqlrcom <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayComand<font color="#990000">)</font>sqlrcon<font color="#990000">.</font><b><font color="#000000">CreateCommand</font></b><font color="#990000">();</font>
sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"begin testproc(:in1,:in2,:in3); end;"</font><font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"in1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"in2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"in3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteNonQuery</font></b><font color="#990000">();</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure testproc
</tt></pre>

</blockquote>
<br/><h4>Sybase and Microsoft SQL Server</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> testproc @<font color="#008080">in1</font> <font color="#009900">int</font><font color="#990000">,</font> @<font color="#008080">in2</font> <font color="#009900">float</font><font color="#990000">,</font> @<font color="#008080">in3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">)</font> as
        insert into <font color="#008080">mytable</font> <b><font color="#000000">values</font></b> <font color="#990000">(</font>@in1<font color="#990000">,</font>@in2<font color="#990000">,</font>@in3<font color="#990000">)</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#008080">SQLRelayCommand</font> sqlrcom <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayComand<font color="#990000">)</font>sqlrcon<font color="#990000">.</font><b><font color="#000000">CreateCommand</font></b><font color="#990000">();</font>
sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"exec testproc"</font><font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"in1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"in2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"in3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteNonQuery</font></b><font color="#990000">();</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure testproc
</tt></pre>

</blockquote>
<h4>Firebird</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">testproc</font></b><font color="#990000">(</font><font color="#008080">in1</font> integer<font color="#990000">,</font> <font color="#008080">in2</font> <font color="#009900">float</font><font color="#990000">,</font> <font color="#008080">in3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font> as
begin
        insert into <font color="#008080">mytable</font> <b><font color="#000000">values</font></b> <font color="#990000">(</font>in1<font color="#990000">,</font>in2<font color="#990000">,</font>in3<font color="#990000">);</font>
        suspend<font color="#990000">;</font>
end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#008080">SQLRelayCommand</font> sqlrcom <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayComand<font color="#990000">)</font>sqlrcon<font color="#990000">.</font><b><font color="#000000">CreateCommand</font></b><font color="#990000">();</font>
sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"execute procedure testproc ?, ?, ?"</font><font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteNonQuery</font></b><font color="#990000">();</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure testproc
</tt></pre>

</blockquote>
<h4>DB2</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">testproc</font></b><font color="#990000">(</font>in <font color="#008080">in1</font> <font color="#009900">int</font><font color="#990000">,</font> in <font color="#008080">in2</font> <font color="#009900">double</font><font color="#990000">,</font> in <font color="#008080">in3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font> language sql
begin
        insert into <font color="#008080">mytable</font> <b><font color="#000000">values</font></b> <font color="#990000">(</font>in1<font color="#990000">,</font>in2<font color="#990000">,</font>in3<font color="#990000">);</font>
end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#008080">SQLRelayCommand</font> sqlrcom <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayComand<font color="#990000">)</font>sqlrcon<font color="#990000">.</font><b><font color="#000000">CreateCommand</font></b><font color="#990000">();</font>
sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"call testproc(?,?,?)"</font><font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteNonQuery</font></b><font color="#990000">();</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure testproc
</tt></pre>

</blockquote>
<h4>Postgresql</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">function</font> <b><font color="#000000">testfunc</font></b><font color="#990000">(</font><font color="#009900">int</font><font color="#990000">,</font><font color="#009900">float</font><font color="#990000">,</font><b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font> returns <font color="#008080">void</font> as <font color="#FF0000">'</font>
<font color="#FF0000">begin</font>
<font color="#FF0000">        insert into mytable values ($1,$2,$3);</font>
<font color="#FF0000">        return;</font>
<font color="#FF0000">end;'</font> language plpgsql
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#008080">SQLRelayCommand</font> sqlrcom <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayComand<font color="#990000">)</font>sqlrcon<font color="#990000">.</font><b><font color="#000000">CreateCommand</font></b><font color="#990000">();</font>
sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"call testfunc($1,$2,$3)"</font><font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteNonQuery</font></b><font color="#990000">();</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop <font color="#008080">function</font> <b><font color="#000000">testfunc</font></b><font color="#990000">(</font><font color="#009900">int</font><font color="#990000">,</font><font color="#009900">float</font><font color="#990000">,</font><b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font>
</tt></pre>

</blockquote>
<h4>MySQL</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">testproc</font></b><font color="#990000">(</font>in <font color="#008080">in1</font> <font color="#009900">int</font><font color="#990000">,</font> in <font color="#008080">in2</font> <font color="#009900">float</font><font color="#990000">,</font> in <font color="#008080">in3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font>
begin
        insert into <font color="#008080">mytable</font> <b><font color="#000000">values</font></b> <font color="#990000">(</font>in1<font color="#990000">,</font>in2<font color="#990000">,</font>in3<font color="#990000">);</font>
end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#008080">SQLRelayCommand</font> sqlrcom <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayComand<font color="#990000">)</font>sqlrcon<font color="#990000">.</font><b><font color="#000000">CreateCommand</font></b><font color="#990000">();</font>
sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"call testproc(?,?,?)"</font><font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteNonQuery</font></b><font color="#990000">();</font>
</tt></pre>

</blockquote>
<p>Note: Versions of MySQL prior to 5.0 had trouble calling stored procedures using bind variables.  If you are using a version of MySQL prior to 5.0 then SQL relay must fake the bind variables and you must use colon-delimited variables (:1, :2, :3, etc.) in your queries rather than the native-mysql queston marks.</p>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure testproc
</tt></pre>

</blockquote>
<br/><h3>Single Values</h3>

<p>Some stored procedures return single values.  Below are examples,
illustrating how to create, execute and drop this kind of stored procedure for
each database that SQL Relay supports.</p>

<h4>Oracle</h4>

<p>In Oracle, stored procedures can return values through output parameters or
as return values of the procedure itself.</p>

<p>Here is an example where the procedure itself returns a value.  Note that
Oracle calls these functions.</p>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">function</font> <b><font color="#000000">testproc</font></b><font color="#990000">(</font>in1 <font color="#008080">in</font> number<font color="#990000">,</font> in2 <font color="#008080">in</font> number<font color="#990000">,</font> in3 <font color="#008080">in</font> varchar2<font color="#990000">)</font> returns number is
begin
        <b><font color="#0000FF">return</font></b> in1<font color="#990000">;</font>
end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#008080">SQLRelayCommand</font> sqlrcom <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayComand<font color="#990000">)</font>sqlrcon<font color="#990000">.</font><b><font color="#000000">CreateCommand</font></b><font color="#990000">();</font>
sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"select testproc(:in1,:in2,:in3) from dual"</font><font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"in1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"in2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"in3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
<font color="#008080">Int64</font> result<font color="#990000">=</font>sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteScalar</font></b><font color="#990000">();</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure testproc
</tt></pre>

</blockquote>
<p>Here is an example where the value is returned through an output
parameter.</p>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">testproc</font></b><font color="#990000">(</font>in1 <font color="#008080">in</font> number<font color="#990000">,</font> in2 <font color="#008080">in</font> number<font color="#990000">,</font> in3 <font color="#008080">in</font> varchar2<font color="#990000">,</font> out1 <font color="#008080">out</font> number<font color="#990000">)</font> as
begin
        out1<font color="#990000">:=</font>in1<font color="#990000">;</font>
end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#008080">SQLRelayCommand</font> sqlrcom <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayComand<font color="#990000">)</font>sqlrcon<font color="#990000">.</font><b><font color="#000000">CreateCommand</font></b><font color="#990000">();</font>
sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"begin testproc(:in1,:in2,:in3,:out1); end"</font><font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"in1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"in2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"in3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
<font color="#008080">SQLRelayParameter</font> out1 <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">SQLRelayParameter</font></b><font color="#990000">();</font>
out1<font color="#990000">.</font>ParameterName <font color="#990000">=</font> <font color="#FF0000">"out1"</font><font color="#990000">;</font>
out1<font color="#990000">.</font>Direction <font color="#990000">=</font> ParameterDirection<font color="#990000">.</font>Output<font color="#990000">;</font>
out1<font color="#990000">.</font>DbType <font color="#990000">=</font> DbType<font color="#990000">.</font>Int64<font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font>out1<font color="#990000">);</font>
sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteNonQuery</font></b><font color="#990000">();</font>
<font color="#008080">Int64</font> result <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToInt64</font></b><font color="#990000">(</font>out1<font color="#990000">.</font>Value<font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure testproc
</tt></pre>

</blockquote>
<br/><h4>Sybase and Microsoft SQL Server</h4>

<p>In Sybase and Microsoft SQL Server, stored procedures return values
through output parameters rather than as return values of the procedure
itself.</p>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> testproc @<font color="#008080">in1</font> <font color="#009900">int</font><font color="#990000">,</font> @<font color="#008080">in2</font> <font color="#009900">float</font><font color="#990000">,</font> @<font color="#008080">in3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">),</font> @out1 <font color="#009900">int</font> output as
        select @out1<font color="#990000">=</font><b><font color="#000000">convert</font></b><font color="#990000">(</font><b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">),</font>@in1<font color="#990000">)</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#008080">SQLRelayCommand</font> sqlrcom <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayComand<font color="#990000">)</font>sqlrcon<font color="#990000">.</font><b><font color="#000000">CreateCommand</font></b><font color="#990000">();</font>
sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"exec testproc"</font><font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"in1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"in2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"in3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
<font color="#008080">SQLRelayParameter</font> out1 <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">SQLRelayParameter</font></b><font color="#990000">();</font>
out1<font color="#990000">.</font>ParameterName <font color="#990000">=</font> <font color="#FF0000">"out1"</font><font color="#990000">;</font>
out1<font color="#990000">.</font>Direction <font color="#990000">=</font> ParameterDirection<font color="#990000">.</font>Output<font color="#990000">;</font>
out1<font color="#990000">.</font>DbType <font color="#990000">=</font> DbType<font color="#990000">.</font>Int64<font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font>out1<font color="#990000">);</font>
sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteNonQuery</font></b><font color="#990000">();</font>
<font color="#008080">Int64</font> result <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToInt64</font></b><font color="#990000">(</font>out1<font color="#990000">.</font>Value<font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure testproc
</tt></pre>

</blockquote>
<br/><h4>Firebird</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">testproc</font></b><font color="#990000">(</font><font color="#008080">in1</font> integer<font color="#990000">,</font> <font color="#008080">in2</font> <font color="#009900">float</font><font color="#990000">,</font> <font color="#008080">in3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font> <b><font color="#000000">returns</font></b> <font color="#990000">(</font><font color="#008080">out1</font> integer<font color="#990000">)</font> as
begin
        out1<font color="#990000">=</font>in1<font color="#990000">;</font>
        suspend<font color="#990000">;</font>
end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#008080">SQLRelayCommand</font> sqlrcom <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayComand<font color="#990000">)</font>sqlrcon<font color="#990000">.</font><b><font color="#000000">CreateCommand</font></b><font color="#990000">();</font>
sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"select * from testproc(?,?,?)"</font><font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
<font color="#008080">Int64</font> result <font color="#990000">=</font> sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteScalar</font></b><font color="#990000">();</font>
</tt></pre>

</blockquote>
<p>Alternatively, you can run a query like the following and receive the result
using an output bind variable.  Note that in Firebird, input and
output bind variable indices are distict from one another.  The index of the
output bind variable is 1 rather than 4, even though there were 3 input bind
variables.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#008080">SQLRelayCommand</font> sqlrcom <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayComand<font color="#990000">)</font>sqlrcon<font color="#990000">.</font><b><font color="#000000">CreateCommand</font></b><font color="#990000">();</font>
sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"execute procedure testproc ?, ?, ?"</font><font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
<font color="#008080">SQLRelayParameter</font> out1 <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">SQLRelayParameter</font></b><font color="#990000">();</font>
out1<font color="#990000">.</font>ParameterName <font color="#990000">=</font> <font color="#FF0000">"1"</font><font color="#990000">;</font>
out1<font color="#990000">.</font>Direction <font color="#990000">=</font> ParameterDirection<font color="#990000">.</font>Output<font color="#990000">;</font>
out1<font color="#990000">.</font>DbType <font color="#990000">=</font> DbType<font color="#990000">.</font>Int64<font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font>out1<font color="#990000">);</font>
sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteNonQuery</font></b><font color="#990000">();</font>
<font color="#008080">Int64</font> result <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToInt64</font></b><font color="#990000">(</font>out1<font color="#990000">.</font>Value<font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure testproc
</tt></pre>

</blockquote>
<br/><h4>DB2</h4>

<p>In DB2, stored procedures return values through output parameters rather
than as return values of the procedure itself.</p>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">testproc</font></b><font color="#990000">(</font>in <font color="#008080">in1</font> <font color="#009900">int</font><font color="#990000">,</font> in <font color="#008080">in2</font> <font color="#009900">double</font><font color="#990000">,</font> in <font color="#008080">in3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">),</font> out <font color="#008080">out1</font> <font color="#009900">int</font><font color="#990000">)</font> language sql
begin
        <font color="#008080">set</font> out1 <font color="#990000">=</font> in1<font color="#990000">;</font>
end
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#008080">SQLRelayCommand</font> sqlrcom <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayComand<font color="#990000">)</font>sqlrcon<font color="#990000">.</font><b><font color="#000000">CreateCommand</font></b><font color="#990000">();</font>
sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"call testproc(?,?,?,?)"</font><font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
<font color="#008080">SQLRelayParameter</font> out4 <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">SQLRelayParameter</font></b><font color="#990000">();</font>
out4<font color="#990000">.</font>ParameterName <font color="#990000">=</font> <font color="#FF0000">"4"</font><font color="#990000">;</font>
out4<font color="#990000">.</font>Direction <font color="#990000">=</font> ParameterDirection<font color="#990000">.</font>Output<font color="#990000">;</font>
out4<font color="#990000">.</font>DbType <font color="#990000">=</font> DbType<font color="#990000">.</font>Int64<font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font>out4<font color="#990000">);</font>
sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteNonQuery</font></b><font color="#990000">();</font>
<font color="#008080">Int64</font> result <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToInt64</font></b><font color="#990000">(</font>out4<font color="#990000">.</font>Value<font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure testproc
</tt></pre>

</blockquote>
<br/><h4>Postgresql</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">function</font> <b><font color="#000000">testfunc</font></b><font color="#990000">(</font><font color="#009900">int</font><font color="#990000">,</font><font color="#009900">float</font><font color="#990000">,</font><font color="#009900">char</font><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font> returns <font color="#008080">int</font> as <font color="#FF0000">'</font>
<font color="#FF0000">declare</font>
<font color="#FF0000">        in1 int;</font>
<font color="#FF0000">        in2 float;</font>
<font color="#FF0000">        in3 char(20);</font>
<font color="#FF0000">begin</font>
<font color="#FF0000">        in1:=$1;</font>
<font color="#FF0000">        return;</font>
<font color="#FF0000">end;</font>
<font color="#FF0000">'</font> language plpgsql
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#008080">SQLRelayCommand</font> sqlrcom <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayComand<font color="#990000">)</font>sqlrcon<font color="#990000">.</font><b><font color="#000000">CreateCommand</font></b><font color="#990000">();</font>
sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"select * from testfunc($1,$2,$3)"</font><font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
<font color="#008080">Int64</font> result <font color="#990000">=</font> sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteScalar</font></b><font color="#990000">();</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop <font color="#008080">function</font> <b><font color="#000000">testfunc</font></b><font color="#990000">(</font><font color="#009900">int</font><font color="#990000">,</font><font color="#009900">float</font><font color="#990000">,</font><font color="#009900">char</font><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font>
</tt></pre>

</blockquote>
<h4>MySQL</h4>

<p>A single value can be returned from a MySQL function.</p>

<p>To create the function, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">function</font> <b><font color="#000000">testfunc</font></b><font color="#990000">(</font>in <font color="#008080">in1</font> <font color="#009900">int</font><font color="#990000">,</font> in <font color="#008080">in2</font> <font color="#009900">float</font><font color="#990000">,</font> in <font color="#008080">in3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font> returns <font color="#009900">int</font> <b><font color="#0000FF">return</font></b> in1<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the function from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#008080">SQLRelayCommand</font> sqlrcom <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayComand<font color="#990000">)</font>sqlrcon<font color="#990000">.</font><b><font color="#000000">CreateCommand</font></b><font color="#990000">();</font>
sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"select * from testfunc(?,?,?)"</font><font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
<font color="#008080">Int64</font> result <font color="#990000">=</font> sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteScalar</font></b><font color="#990000">();</font>
</tt></pre>

</blockquote>
<p>Note: Versions of MySQL prior to 5.0 had trouble calling stored procedures using bind variables.  If you are using a version of MySQL prior to 5.0 then SQL relay must fake the bind variables and you must use colon-delimited variables (:1, :2, :3, etc.) in your queries rather than the native-mysql queston marks.</p>

<p>To drop the function, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure testproc
</tt></pre>

</blockquote>
<p>A single value can be returned in the result set of a MySQL procedure.</p>

<p>To create the procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">testproc</font></b><font color="#990000">()</font> begin select <font color="#993399">1</font><font color="#990000">;</font> end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#008080">SQLRelayCommand</font> sqlrcom <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayComand<font color="#990000">)</font>sqlrcon<font color="#990000">.</font><b><font color="#000000">CreateCommand</font></b><font color="#990000">();</font>
sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"select testproc()"</font><font color="#990000">;</font>
<font color="#008080">Int64</font> result <font color="#990000">=</font> sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteScalar</font></b><font color="#990000">();</font>
</tt></pre>

</blockquote>
<p>To drop the procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure testproc
</tt></pre>

</blockquote>
<p>A single value can be returned using the output variable of a MySQL
procedure.</p>

<p>To create the procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">testproc</font></b><font color="#990000">(</font>out <font color="#008080">out1</font> <font color="#009900">int</font><font color="#990000">)</font> begin select <font color="#993399">1</font> <font color="#008080">into</font> out1<font color="#990000">;</font> end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#008080">SQLRelayCommand</font> sqlrcom <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayComand<font color="#990000">)</font>sqlrcon<font color="#990000">.</font><b><font color="#000000">CreateCommand</font></b><font color="#990000">();</font>

sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"set @out1=0"</font><font color="#990000">;</font>
<font color="#008080">Int64</font> result <font color="#990000">=</font> sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteNonQuery</font></b><font color="#990000">();</font>

sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"call testproc(@out1)"</font><font color="#990000">;</font>
<font color="#008080">Int64</font> result <font color="#990000">=</font> sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteNonQuery</font></b><font color="#990000">();</font>

sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"select @out1"</font><font color="#990000">;</font>
<font color="#008080">Int64</font> result <font color="#990000">=</font> sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteScalar</font></b><font color="#990000">();</font>
</tt></pre>

</blockquote>
<p>To drop the procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure testproc
</tt></pre>

</blockquote>
<br/><br/><h3>Multiple Values</h3>

<p>Some stored procedures return multiple values.  Below are examples,
illustrating how to create, execute and drop this kind of stored procedure for
each database that SQL Relay supports.</p>

<h4>Oracle</h4>

<p>In Oracle, stored procedures can return values through output parameters or
as return values of the procedure itself.  If a procedure needs to return
multiple values, it can return one of them as the return value of the procedure
itself, but the rest must be returned through output parameters.</p>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">testproc</font></b><font color="#990000">(</font>in1 <font color="#008080">in</font> number<font color="#990000">,</font> in2 <font color="#008080">in</font> number<font color="#990000">,</font> in3 <font color="#008080">in</font> varchar2<font color="#990000">,</font> out1 <font color="#008080">out</font> number<font color="#990000">,</font> out2 <font color="#008080">out</font> number<font color="#990000">,</font> out3 <font color="#008080">out</font> varchar2<font color="#990000">)</font> is
begin
        out1<font color="#990000">:=</font>in1<font color="#990000">;</font>
        out2<font color="#990000">:=</font>in2<font color="#990000">;</font>
        out3<font color="#990000">:=</font>in3<font color="#990000">;</font>
end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#008080">SQLRelayCommand</font> sqlrcom <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayComand<font color="#990000">)</font>sqlrcon<font color="#990000">.</font><b><font color="#000000">CreateCommand</font></b><font color="#990000">();</font>
sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"begin testproc(:in1,:in2,:in3,:out1,:out2,:out3); end;"</font><font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"in1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"in2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"in3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
<font color="#008080">SQLRelayParameter</font> out1 <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">SQLRelayParameter</font></b><font color="#990000">();</font>
out1<font color="#990000">.</font>ParameterName <font color="#990000">=</font> <font color="#FF0000">"out1"</font><font color="#990000">;</font>
out1<font color="#990000">.</font>Direction <font color="#990000">=</font> ParameterDirection<font color="#990000">.</font>Output<font color="#990000">;</font>
out1<font color="#990000">.</font>DbType <font color="#990000">=</font> DbType<font color="#990000">.</font>Int64<font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font>out1<font color="#990000">);</font>
<font color="#008080">SQLRelayParameter</font> out2 <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">SQLRelayParameter</font></b><font color="#990000">();</font>
out2<font color="#990000">.</font>ParameterName <font color="#990000">=</font> <font color="#FF0000">"out2"</font><font color="#990000">;</font>
out2<font color="#990000">.</font>Direction <font color="#990000">=</font> ParameterDirection<font color="#990000">.</font>Output<font color="#990000">;</font>
out2<font color="#990000">.</font>DbType <font color="#990000">=</font> DbType<font color="#990000">.</font>Double<font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font>out2<font color="#990000">);</font>
<font color="#008080">SQLRelayParameter</font> out2 <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">SQLRelayParameter</font></b><font color="#990000">();</font>
out3<font color="#990000">.</font>ParameterName <font color="#990000">=</font> <font color="#FF0000">"out3"</font><font color="#990000">;</font>
out3<font color="#990000">.</font>Direction <font color="#990000">=</font> ParameterDirection<font color="#990000">.</font>Output<font color="#990000">;</font>
out3<font color="#990000">.</font>DbType <font color="#990000">=</font> DbType<font color="#990000">.</font>String<font color="#990000">;</font>
out3<font color="#990000">.</font>Size <font color="#990000">=</font> <font color="#993399">20</font><font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font>out3<font color="#990000">);</font>
sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteNonQuery</font></b><font color="#990000">();</font>
<font color="#008080">Int64</font> result1 <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToInt64</font></b><font color="#990000">(</font>out1<font color="#990000">.</font>Value<font color="#990000">);</font>
<font color="#008080">Double</font> result2 <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToDouble</font></b><font color="#990000">(</font>out2<font color="#990000">.</font>Value<font color="#990000">);</font>
<font color="#008080">String</font> result3 <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToString</font></b><font color="#990000">(</font>out3<font color="#990000">.</font>Value<font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure testproc
</tt></pre>

</blockquote>
<br/><h4>Sybase and Microsoft SQL Server</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> testproc @<font color="#008080">in1</font> <font color="#009900">int</font><font color="#990000">,</font> @<font color="#008080">in2</font> <font color="#009900">float</font><font color="#990000">,</font> @<font color="#008080">in3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">),</font> @out1 <font color="#008080">int</font> output<font color="#990000">,</font> @out2 <font color="#008080">int</font> output<font color="#990000">,</font> @out3 <font color="#009900">int</font> output as
        select @out1<font color="#990000">=</font><b><font color="#000000">convert</font></b><font color="#990000">(</font><b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">),</font>@in1<font color="#990000">),</font>
                @out2<font color="#990000">=</font><b><font color="#000000">convert</font></b><font color="#990000">(</font><b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">),</font>@in2<font color="#990000">),</font>
                @out2<font color="#990000">=</font><b><font color="#000000">convert</font></b><font color="#990000">(</font><b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">),</font>@in2<font color="#990000">)</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#008080">SQLRelayCommand</font> sqlrcom <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayComand<font color="#990000">)</font>sqlrcon<font color="#990000">.</font><b><font color="#000000">CreateCommand</font></b><font color="#990000">();</font>
sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"exec testproc"</font><font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"in1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"in2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"in3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
<font color="#008080">SQLRelayParameter</font> out1 <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">SQLRelayParameter</font></b><font color="#990000">();</font>
out1<font color="#990000">.</font>ParameterName <font color="#990000">=</font> <font color="#FF0000">"out1"</font><font color="#990000">;</font>
out1<font color="#990000">.</font>Direction <font color="#990000">=</font> ParameterDirection<font color="#990000">.</font>Output<font color="#990000">;</font>
out1<font color="#990000">.</font>DbType <font color="#990000">=</font> DbType<font color="#990000">.</font>Int64<font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font>out1<font color="#990000">);</font>
<font color="#008080">SQLRelayParameter</font> out2 <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">SQLRelayParameter</font></b><font color="#990000">();</font>
out2<font color="#990000">.</font>ParameterName <font color="#990000">=</font> <font color="#FF0000">"out2"</font><font color="#990000">;</font>
out2<font color="#990000">.</font>Direction <font color="#990000">=</font> ParameterDirection<font color="#990000">.</font>Output<font color="#990000">;</font>
out2<font color="#990000">.</font>DbType <font color="#990000">=</font> DbType<font color="#990000">.</font>Double<font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font>out2<font color="#990000">);</font>
<font color="#008080">SQLRelayParameter</font> out2 <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">SQLRelayParameter</font></b><font color="#990000">();</font>
out3<font color="#990000">.</font>ParameterName <font color="#990000">=</font> <font color="#FF0000">"out3"</font><font color="#990000">;</font>
out3<font color="#990000">.</font>Direction <font color="#990000">=</font> ParameterDirection<font color="#990000">.</font>Output<font color="#990000">;</font>
out3<font color="#990000">.</font>DbType <font color="#990000">=</font> DbType<font color="#990000">.</font>String<font color="#990000">;</font>
out3<font color="#990000">.</font>Size <font color="#990000">=</font> <font color="#993399">20</font><font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font>out3<font color="#990000">);</font>
sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteNonQuery</font></b><font color="#990000">();</font>
<font color="#008080">Int64</font> result1 <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToInt64</font></b><font color="#990000">(</font>out1<font color="#990000">.</font>Value<font color="#990000">);</font>
<font color="#008080">Double</font> result2 <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToDouble</font></b><font color="#990000">(</font>out2<font color="#990000">.</font>Value<font color="#990000">);</font>
<font color="#008080">String</font> result3 <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToString</font></b><font color="#990000">(</font>out3<font color="#990000">.</font>Value<font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure testproc
</tt></pre>

</blockquote>
<br/><h4>Firebird</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">testproc</font></b><font color="#990000">(</font><font color="#008080">in1</font> integer<font color="#990000">,</font> <font color="#008080">in2</font> <font color="#009900">float</font><font color="#990000">,</font> <font color="#008080">in3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font> <b><font color="#000000">returns</font></b> <font color="#990000">(</font><font color="#008080">out1</font> integer<font color="#990000">,</font> <font color="#008080">out2</font> <font color="#009900">float</font><font color="#990000">,</font> <font color="#008080">out3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font> as
begin
        out1<font color="#990000">=</font>in1<font color="#990000">;</font>
        out2<font color="#990000">=</font>in2<font color="#990000">;</font>
        out3<font color="#990000">=</font>in3<font color="#990000">;</font>
        suspend<font color="#990000">;</font>
end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#008080">SQLRelayCommand</font> sqlrcom <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayComand<font color="#990000">)</font>sqlrcon<font color="#990000">.</font><b><font color="#000000">CreateCommand</font></b><font color="#990000">();</font>
sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"select * from testproc(?,?,?)"</font><font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteReader</font></b><font color="#990000">();</font>
System<font color="#990000">.</font>Data<font color="#990000">.</font><font color="#008080">IDataReader</font> datareader <font color="#990000">=</font> sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteReader</font></b><font color="#990000">();</font>
datareader<font color="#990000">.</font><b><font color="#000000">Read</font></b><font color="#990000">();</font>
<font color="#008080">Int64</font> out1 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetInt64</font></b><font color="#990000">(</font><font color="#993399">0</font><font color="#990000">);</font>
<font color="#008080">Double</font> out2 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetDouble</font></b><font color="#990000">(</font><font color="#993399">1</font><font color="#990000">);</font>
<font color="#008080">String</font> out3 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetString</font></b><font color="#990000">(</font><font color="#993399">2</font><font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>Alternatively, you can run a query like the following and receive the result
using a output bind variables.  Note that in Firebird, input and
output bind variable indices are distict from one another.  The index of the
first output bind variable is 1 rather than 4, even though there were 3 input
bind variables.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#008080">SQLRelayCommand</font> sqlrcom <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayComand<font color="#990000">)</font>sqlrcon<font color="#990000">.</font><b><font color="#000000">CreateCommand</font></b><font color="#990000">();</font>
sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"exec procedure testproc ?, ?, ?"</font><font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
<font color="#008080">SQLRelayParameter</font> out1 <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">SQLRelayParameter</font></b><font color="#990000">();</font>
out1<font color="#990000">.</font>ParameterName <font color="#990000">=</font> <font color="#FF0000">"1"</font><font color="#990000">;</font>
out1<font color="#990000">.</font>Direction <font color="#990000">=</font> ParameterDirection<font color="#990000">.</font>Output<font color="#990000">;</font>
out1<font color="#990000">.</font>DbType <font color="#990000">=</font> DbType<font color="#990000">.</font>Int64<font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font>out1<font color="#990000">);</font>
<font color="#008080">SQLRelayParameter</font> out2 <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">SQLRelayParameter</font></b><font color="#990000">();</font>
out2<font color="#990000">.</font>ParameterName <font color="#990000">=</font> <font color="#FF0000">"2"</font><font color="#990000">;</font>
out2<font color="#990000">.</font>Direction <font color="#990000">=</font> ParameterDirection<font color="#990000">.</font>Output<font color="#990000">;</font>
out2<font color="#990000">.</font>DbType <font color="#990000">=</font> DbType<font color="#990000">.</font>Double<font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font>out2<font color="#990000">);</font>
<font color="#008080">SQLRelayParameter</font> out2 <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">SQLRelayParameter</font></b><font color="#990000">();</font>
out3<font color="#990000">.</font>ParameterName <font color="#990000">=</font> <font color="#FF0000">"3"</font><font color="#990000">;</font>
out3<font color="#990000">.</font>Direction <font color="#990000">=</font> ParameterDirection<font color="#990000">.</font>Output<font color="#990000">;</font>
out3<font color="#990000">.</font>DbType <font color="#990000">=</font> DbType<font color="#990000">.</font>String<font color="#990000">;</font>
out3<font color="#990000">.</font>Size <font color="#990000">=</font> <font color="#993399">20</font><font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font>out3<font color="#990000">);</font>
sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteNonQuery</font></b><font color="#990000">();</font>
<font color="#008080">Int64</font> result1 <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToInt64</font></b><font color="#990000">(</font>out1<font color="#990000">.</font>Value<font color="#990000">);</font>
<font color="#008080">Double</font> result2 <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToDouble</font></b><font color="#990000">(</font>out2<font color="#990000">.</font>Value<font color="#990000">);</font>
<font color="#008080">String</font> result3 <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToString</font></b><font color="#990000">(</font>out3<font color="#990000">.</font>Value<font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure testproc
</tt></pre>

</blockquote>
<br/><h4>DB2</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">testproc</font></b><font color="#990000">(</font>in <font color="#008080">in1</font> <font color="#009900">int</font><font color="#990000">,</font> in <font color="#008080">in2</font> <font color="#009900">double</font><font color="#990000">,</font> in <font color="#008080">in3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">),</font> out <font color="#008080">out1</font> <font color="#009900">int</font><font color="#990000">,</font> out <font color="#008080">out2</font> <font color="#009900">double</font><font color="#990000">,</font> out <font color="#008080">out3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font> language sql
begin
        <font color="#008080">set</font> out1 <font color="#990000">=</font> in1<font color="#990000">;</font>
        <font color="#008080">set</font> out2 <font color="#990000">=</font> in2<font color="#990000">;</font>
        <font color="#008080">set</font> out3 <font color="#990000">=</font> in3<font color="#990000">;</font>
end
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#008080">SQLRelayCommand</font> sqlrcom <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayComand<font color="#990000">)</font>sqlrcon<font color="#990000">.</font><b><font color="#000000">CreateCommand</font></b><font color="#990000">();</font>
sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"call testproc(?,?,?,?,?,?)"</font><font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
<font color="#008080">SQLRelayParameter</font> out1 <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">SQLRelayParameter</font></b><font color="#990000">();</font>
out1<font color="#990000">.</font>ParameterName <font color="#990000">=</font> <font color="#FF0000">"4"</font><font color="#990000">;</font>
out1<font color="#990000">.</font>Direction <font color="#990000">=</font> ParameterDirection<font color="#990000">.</font>Output<font color="#990000">;</font>
out1<font color="#990000">.</font>DbType <font color="#990000">=</font> DbType<font color="#990000">.</font>Int64<font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font>out1<font color="#990000">);</font>
<font color="#008080">SQLRelayParameter</font> out2 <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">SQLRelayParameter</font></b><font color="#990000">();</font>
out2<font color="#990000">.</font>ParameterName <font color="#990000">=</font> <font color="#FF0000">"5"</font><font color="#990000">;</font>
out2<font color="#990000">.</font>Direction <font color="#990000">=</font> ParameterDirection<font color="#990000">.</font>Output<font color="#990000">;</font>
out2<font color="#990000">.</font>DbType <font color="#990000">=</font> DbType<font color="#990000">.</font>Double<font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font>out2<font color="#990000">);</font>
<font color="#008080">SQLRelayParameter</font> out2 <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">SQLRelayParameter</font></b><font color="#990000">();</font>
out3<font color="#990000">.</font>ParameterName <font color="#990000">=</font> <font color="#FF0000">"6"</font><font color="#990000">;</font>
out3<font color="#990000">.</font>Direction <font color="#990000">=</font> ParameterDirection<font color="#990000">.</font>Output<font color="#990000">;</font>
out3<font color="#990000">.</font>DbType <font color="#990000">=</font> DbType<font color="#990000">.</font>String<font color="#990000">;</font>
out3<font color="#990000">.</font>Size <font color="#990000">=</font> <font color="#993399">20</font><font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font>out3<font color="#990000">);</font>
sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteNonQuery</font></b><font color="#990000">();</font>
<font color="#008080">Int64</font> result1 <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToInt64</font></b><font color="#990000">(</font>out1<font color="#990000">.</font>Value<font color="#990000">);</font>
<font color="#008080">Double</font> result2 <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToDouble</font></b><font color="#990000">(</font>out2<font color="#990000">.</font>Value<font color="#990000">);</font>
<font color="#008080">String</font> result3 <font color="#990000">=</font> Convert<font color="#990000">.</font><b><font color="#000000">ToString</font></b><font color="#990000">(</font>out3<font color="#990000">.</font>Value<font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure testproc
</tt></pre>

</blockquote>
<br/><h4>Postgresql</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">function</font> <b><font color="#000000">testfunc</font></b><font color="#990000">(</font><font color="#009900">int</font><font color="#990000">,</font><font color="#009900">float</font><font color="#990000">,</font><font color="#009900">char</font><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font> returns <font color="#008080">record</font> as <font color="#FF0000">'</font>
<font color="#FF0000">declare</font>
<font color="#FF0000">        output record;</font>
<font color="#FF0000">begin</font>
<font color="#FF0000">        select $1,$2,$3 into output;</font>
<font color="#FF0000">        return output;</font>
<font color="#FF0000">end;</font>
<font color="#FF0000">'</font> language plpgsql
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#008080">SQLRelayCommand</font> sqlrcom <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayComand<font color="#990000">)</font>sqlrcon<font color="#990000">.</font><b><font color="#000000">CreateCommand</font></b><font color="#990000">();</font>
sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"select * from testfunc(?,?,?) as (col1 int, col2 float, col3 char(20))"</font><font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"in1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"in2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"in3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteReader</font></b><font color="#990000">();</font>
System<font color="#990000">.</font>Data<font color="#990000">.</font><font color="#008080">IDataReader</font> datareader <font color="#990000">=</font> sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteReader</font></b><font color="#990000">();</font>
datareader<font color="#990000">.</font><b><font color="#000000">Read</font></b><font color="#990000">();</font>
<font color="#008080">Int64</font> out1 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetInt64</font></b><font color="#990000">(</font><font color="#993399">0</font><font color="#990000">);</font>
<font color="#008080">Double</font> out2 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetDouble</font></b><font color="#990000">(</font><font color="#993399">1</font><font color="#990000">);</font>
<font color="#008080">String</font> out3 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetString</font></b><font color="#990000">(</font><font color="#993399">2</font><font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop <font color="#008080">function</font> <b><font color="#000000">testfunc</font></b><font color="#990000">(</font><font color="#009900">int</font><font color="#990000">,</font><font color="#009900">float</font><font color="#990000">,</font><font color="#009900">char</font><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font>
</tt></pre>

</blockquote>
<h4>MySQL</h4>

<p>Here's how you can get multiple values from the result
set of a MySQL procedure.</p>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">testproc</font></b><font color="#990000">(</font>in <font color="#008080">in1</font> <font color="#009900">int</font><font color="#990000">,</font> in <font color="#008080">in2</font> <font color="#009900">float</font><font color="#990000">,</font> in <font color="#008080">in3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font> begin <font color="#008080">select</font> in1<font color="#990000">,</font> in2<font color="#990000">,</font> in3<font color="#990000">;</font> end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#008080">SQLRelayCommand</font> sqlrcom <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayComand<font color="#990000">)</font>sqlrcon<font color="#990000">.</font><b><font color="#000000">CreateCommand</font></b><font color="#990000">();</font>
sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"call testproc(?,?,?)"</font><font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"1"</font><font color="#990000">,</font><font color="#993399">1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"2"</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font><font color="#FF0000">"3"</font><font color="#990000">,</font><font color="#FF0000">"hello"</font><font color="#990000">);</font>
sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteReader</font></b><font color="#990000">();</font>
System<font color="#990000">.</font>Data<font color="#990000">.</font><font color="#008080">IDataReader</font> datareader <font color="#990000">=</font> sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteReader</font></b><font color="#990000">();</font>
datareader<font color="#990000">.</font><b><font color="#000000">Read</font></b><font color="#990000">();</font>
<font color="#008080">Int64</font> out1 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetInt64</font></b><font color="#990000">(</font><font color="#993399">0</font><font color="#990000">);</font>
<font color="#008080">Double</font> out2 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetDouble</font></b><font color="#990000">(</font><font color="#993399">1</font><font color="#990000">);</font>
<font color="#008080">String</font> out3 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetString</font></b><font color="#990000">(</font><font color="#993399">2</font><font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>Note: Versions of MySQL prior to 5.0 had trouble calling stored procedures using bind variables.  If you are using a version of MySQL prior to 5.0 then SQL relay must fake the bind variables and you must use colon-delimited variables (:1, :2, :3, etc.) in your queries rather than the native-mysql queston marks.</p>

<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure testproc
</tt></pre>

</blockquote>
<p>Here's how you can get multiple values from the output variables
of a MySQL procedure.</p>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">testproc</font></b><font color="#990000">(</font>out <font color="#008080">out1</font> <font color="#009900">int</font><font color="#990000">,</font> out <font color="#008080">out2</font> <font color="#009900">float</font><font color="#990000">,</font> out <font color="#008080">out3</font> <b><font color="#000000">varchar</font></b><font color="#990000">(</font><font color="#993399">20</font><font color="#990000">))</font> begin select <font color="#993399">1</font><font color="#990000">,</font><font color="#993399">1.1</font><font color="#990000">,</font><font color="#FF0000">'hello'</font> <font color="#008080">into</font> out1<font color="#990000">,</font> out2<font color="#990000">,</font> out3<font color="#990000">;</font> end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#008080">SQLRelayCommand</font> sqlrcom <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayComand<font color="#990000">)</font>sqlrcon<font color="#990000">.</font><b><font color="#000000">CreateCommand</font></b><font color="#990000">();</font>

sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"set @out1=0, @out2=0.0, @out3=''"</font><font color="#990000">;</font>
<font color="#008080">Int64</font> result <font color="#990000">=</font> sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteNonQuery</font></b><font color="#990000">();</font>

sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"call testproc(@out1,@out2,@out3)"</font><font color="#990000">;</font>
<font color="#008080">Int64</font> result <font color="#990000">=</font> sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteNonQuery</font></b><font color="#990000">();</font>

sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"select @out1,@out2,@out3"</font><font color="#990000">;</font>
System<font color="#990000">.</font>Data<font color="#990000">.</font><font color="#008080">IDataReader</font> datareader <font color="#990000">=</font> sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteReader</font></b><font color="#990000">();</font>
datareader<font color="#990000">.</font><b><font color="#000000">Read</font></b><font color="#990000">();</font>
<font color="#008080">Int64</font> out1 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetInt64</font></b><font color="#990000">(</font><font color="#993399">0</font><font color="#990000">);</font>
<font color="#008080">Double</font> out2 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetDouble</font></b><font color="#990000">(</font><font color="#993399">1</font><font color="#990000">);</font>
<font color="#008080">String</font> out3 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetString</font></b><font color="#990000">(</font><font color="#993399">2</font><font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure testproc
</tt></pre>

</blockquote>
<br/><h3>Result Sets</h3>

<p>Some stored procedures return entire result sets.  Below are examples,
illustrating how to create, execute and drop this kind of stored procedure for
each database that SQL Relay supports.</p>

<h4>Oracle</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create or replace package types as
        type cursorType is <font color="#008080">ref</font> cursor<font color="#990000">;</font>
end<font color="#990000">;</font>

create function testproc <font color="#008080">return</font> types<font color="#990000">.</font>cursortype is
        <font color="#008080">l_cursor</font>    types<font color="#990000">.</font>cursorType<font color="#990000">;</font>
begin
        <font color="#008080">open</font> l_cursor <font color="#008080">for</font> select <font color="#990000">*</font> <font color="#008080">from</font> mytable<font color="#990000">;</font>
        <b><font color="#0000FF">return</font></b> l_cursor<font color="#990000">;</font>
end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>In Oracle, for a result set to be fetched from a stored procedure, the result set must be bound to an output parameter of type cursor.  That cursor may then be opened and fetched from.  In fact, a stored procedure may return multiple result sets and each may be bound to a cursor.</p>

<p>ADO.NET abstracts this with the "multiple result sets" paradigm.  Output cursors must be defined when the query is executed.  Once the main query has been executed, <a href="NextResult">NextResult</a> may be called to step through the result sets bound to each cursor.</p>

<p>For example:</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#008080">SQLRelayCommand</font> sqlrcom <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayComand<font color="#990000">)</font>sqlrcon<font color="#990000">.</font><b><font color="#000000">CreateCommand</font></b><font color="#990000">();</font>
sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"begin :curs=testproc; end;"</font><font color="#990000">;</font>

<font color="#008080">SQLRelayParameter</font> curs <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">SQLRelayParameter</font></b><font color="#990000">();</font>
curs<font color="#990000">.</font>ParameterName <font color="#990000">=</font> <font color="#FF0000">"curs"</font><font color="#990000">;</font>
curs<font color="#990000">.</font>Direction <font color="#990000">=</font> ParameterDirection<font color="#990000">.</font>Output<font color="#990000">;</font>
curs<font color="#990000">.</font>SQLRelayType <font color="#990000">=</font> SQLRelayType<font color="#990000">.</font>Cursor<font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font>curs<font color="#990000">);</font>

System<font color="#990000">.</font>Data<font color="#990000">.</font><font color="#008080">IDataReader</font> datareader <font color="#990000">=</font> sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteReader</font></b><font color="#990000">();</font>

<i><font color="#9A1900">// the main result set will be empty, skip to the result set of the cursor</font></i>
datareader<font color="#990000">.</font><b><font color="#000000">NextResult</font></b><font color="#990000">();</font>

datareader<font color="#990000">.</font><b><font color="#000000">Read</font></b><font color="#990000">();</font>
<font color="#008080">Int64</font> out1 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetInt64</font></b><font color="#990000">(</font><font color="#993399">0</font><font color="#990000">);</font>
<font color="#008080">Double</font> out2 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetDouble</font></b><font color="#990000">(</font><font color="#993399">1</font><font color="#990000">);</font>
<font color="#008080">String</font> out3 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetString</font></b><font color="#990000">(</font><font color="#993399">2</font><font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>Note that in the above code, the Parameter.SQLRelayType must be set rather than Parameter.DbType, which would generally be set.  DbType is constrained to a set of values defined in the ADO.NET spec, which does not include Cursor types.</p>

<p>This approach has some drawbacks though.  Mainly, if a stored procedure returns multiple results sets then they must be accessed sequentially.  The SQL Relay ADO.NET adapter provides another way of accessing the result sets of a stored procedure.  After execution, the Value of each cursor parameter may be cast to a SQLRelayDataReader and data may be fetched from it.  This approach allows data to be fetched from any result set, in any order.</p>

<p>For example:</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><font color="#008080">SQLRelayCommand</font> sqlrcom <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayComand<font color="#990000">)</font>sqlrcon<font color="#990000">.</font><b><font color="#000000">CreateCommand</font></b><font color="#990000">();</font>
sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"begin :curs=testproc; end;"</font><font color="#990000">;</font>

<font color="#008080">SQLRelayParameter</font> curs <font color="#990000">=</font> <b><font color="#0000FF">new</font></b> <b><font color="#000000">SQLRelayParameter</font></b><font color="#990000">();</font>
curs<font color="#990000">.</font>ParameterName <font color="#990000">=</font> <font color="#FF0000">"curs"</font><font color="#990000">;</font>
curs<font color="#990000">.</font>Direction <font color="#990000">=</font> ParameterDirection<font color="#990000">.</font>Output<font color="#990000">;</font>
curs<font color="#990000">.</font>SQLRelayType <font color="#990000">=</font> SQLRelayType<font color="#990000">.</font>Cursor<font color="#990000">;</font>
sqlrcom<font color="#990000">.</font>Parameters<font color="#990000">.</font><b><font color="#000000">Add</font></b><font color="#990000">(</font>curs<font color="#990000">);</font>

sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteNonQuery</font></b><font color="#990000">();</font>

<font color="#008080">SQLRelayDataReader</font> datareader <font color="#990000">=</font> <font color="#990000">(</font>SQLRelayDataReader<font color="#990000">)</font>curs<font color="#990000">.</font>Value<font color="#990000">;</font>

datareader<font color="#990000">.</font><b><font color="#000000">Read</font></b><font color="#990000">();</font>
<font color="#008080">Int64</font> out1 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetInt64</font></b><font color="#990000">(</font><font color="#993399">0</font><font color="#990000">);</font>
<font color="#008080">Double</font> out2 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetDouble</font></b><font color="#990000">(</font><font color="#993399">1</font><font color="#990000">);</font>
<font color="#008080">String</font> out3 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetString</font></b><font color="#990000">(</font><font color="#993399">2</font><font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop function testproc
drop package types
</tt></pre>

</blockquote>
<h4>Sybase and Microsoft SQL Server</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create procedure testproc <font color="#008080">as</font> select <font color="#990000">*</font> from testtable
</tt></pre>

</blockquote>
<p>To exceute the stored procedure from an SQL Relay program, ue code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"exec testproc"</font><font color="#990000">;</font>
System<font color="#990000">.</font>Data<font color="#990000">.</font><font color="#008080">IDataReader</font> datareader <font color="#990000">=</font> sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteReader</font></b><font color="#990000">();</font>
datareader<font color="#990000">.</font><b><font color="#000000">Read</font></b><font color="#990000">();</font>
<font color="#008080">Int64</font> out1 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetInt64</font></b><font color="#990000">(</font><font color="#993399">0</font><font color="#990000">);</font>
<font color="#008080">Double</font> out2 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetDouble</font></b><font color="#990000">(</font><font color="#993399">1</font><font color="#990000">);</font>
<font color="#008080">String</font> out3 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetString</font></b><font color="#990000">(</font><font color="#993399">2</font><font color="#990000">);</font>
datareader<font color="#990000">.</font><b><font color="#000000">Read</font></b><font color="#990000">();</font>
out1 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetInt64</font></b><font color="#990000">(</font><font color="#993399">0</font><font color="#990000">);</font>
out2 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetDouble</font></b><font color="#990000">(</font><font color="#993399">1</font><font color="#990000">);</font>
out3 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetString</font></b><font color="#990000">(</font><font color="#993399">2</font><font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure testproc
</tt></pre>

</blockquote>
<br/><h4>Firebird</h4>

<p>Stored procedures in Firebird can return a result set if a
select query in the procedure selects values into the output parameters and
then issues a suspend command, however SQL Relay doesn't currently support
stored procedures that return result sets.</p>

<h4>DB2</h4>

<p>Stored procedures in DB2 can return a result set if the procedure is declared
to return one, however SQL Relay doesn't currently support stored procedures
that return result sets.</p>

<h4>Postgresql</h4>

<p>To create the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">function</font> <b><font color="#000000">testfunc</font></b><font color="#990000">()</font> returns setof <font color="#008080">record</font> as <font color="#FF0000">'</font>
<font color="#FF0000">        declare output record;</font>
<font color="#FF0000">begin</font>
<font color="#FF0000">        for output in select * from mytable loop</font>
<font color="#FF0000">                return next output;</font>
<font color="#FF0000">        end loop;</font>
<font color="#FF0000">        return;</font>
<font color="#FF0000">end;</font>
<font color="#FF0000">'</font> language plpgsql
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"select * from testfunc() as (testint int, testfloat float, testchar char(40))"</font><font color="#990000">;</font>
System<font color="#990000">.</font>Data<font color="#990000">.</font><font color="#008080">IDataReader</font> datareader <font color="#990000">=</font> sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteReader</font></b><font color="#990000">();</font>
datareader<font color="#990000">.</font><b><font color="#000000">Read</font></b><font color="#990000">();</font>
<font color="#008080">Int64</font> out1 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetInt64</font></b><font color="#990000">(</font><font color="#993399">0</font><font color="#990000">);</font>
<font color="#008080">Double</font> out2 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetDouble</font></b><font color="#990000">(</font><font color="#993399">1</font><font color="#990000">);</font>
<font color="#008080">String</font> out3 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetString</font></b><font color="#990000">(</font><font color="#993399">2</font><font color="#990000">);</font>
datareader<font color="#990000">.</font><b><font color="#000000">Read</font></b><font color="#990000">();</font>
out1 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetInt64</font></b><font color="#990000">(</font><font color="#993399">0</font><font color="#990000">);</font>
out2 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetDouble</font></b><font color="#990000">(</font><font color="#993399">1</font><font color="#990000">);</font>
out3 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetString</font></b><font color="#990000">(</font><font color="#993399">2</font><font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop function testfunc
</tt></pre>

</blockquote>
<h4>MySQL</h4>

<p>The result sets of all select statements called within MySQL stored
procedures (that aren't selected into variables) are returned from the procedure
call.  Though MySQL stored procedures can return multiple result sets,
currently SQL Relay can only fetch the first result set.</p>

<p>To create the stored procedure which returns a result set, run a query like
the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>create <font color="#008080">procedure</font> <b><font color="#000000">testproc</font></b><font color="#990000">()</font> <font color="#008080">begin</font> select <font color="#990000">*</font> <font color="#008080">from</font> mytable<font color="#990000">;</font> end<font color="#990000">;</font>
</tt></pre>

</blockquote>
<p>To execute the stored procedure from an SQL Relay program, use code like the
following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>sqlrcom<font color="#990000">.</font>CommandText <font color="#990000">=</font> <font color="#FF0000">"call testproc()"</font><font color="#990000">;</font>
System<font color="#990000">.</font>Data<font color="#990000">.</font><font color="#008080">IDataReader</font> datareader <font color="#990000">=</font> sqlrcom<font color="#990000">.</font><b><font color="#000000">ExecuteReader</font></b><font color="#990000">();</font>
datareader<font color="#990000">.</font><b><font color="#000000">Read</font></b><font color="#990000">();</font>
<font color="#008080">Int64</font> out1 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetInt64</font></b><font color="#990000">(</font><font color="#993399">0</font><font color="#990000">);</font>
<font color="#008080">Double</font> out2 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetDouble</font></b><font color="#990000">(</font><font color="#993399">1</font><font color="#990000">);</font>
<font color="#008080">String</font> out3 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetString</font></b><font color="#990000">(</font><font color="#993399">2</font><font color="#990000">);</font>
datareader<font color="#990000">.</font><b><font color="#000000">Read</font></b><font color="#990000">();</font>
out1 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetInt64</font></b><font color="#990000">(</font><font color="#993399">0</font><font color="#990000">);</font>
out2 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetDouble</font></b><font color="#990000">(</font><font color="#993399">1</font><font color="#990000">);</font>
out3 <font color="#990000">=</font> datareader<font color="#990000">.</font><b><font color="#000000">GetString</font></b><font color="#990000">(</font><font color="#993399">2</font><font color="#990000">);</font>
</tt></pre>

</blockquote>
<p>To drop the stored procedure, run a query like the following.</p>

<blockquote>
<!-- Generator: GNU source-highlight 3.1.8
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt>drop procedure testproc
</tt></pre>

</blockquote>
<a name="lastinsertid"/><h2>Getting the Last Insert ID</h2>

<p>Databases with autoincrement or identity columns often provide functions which return the "last insert id"; the value of the autoincrement column that was generated during the insert into the database.</p>

<p>Unfortunately ADO.NET doesn't expose a generic method for getting the last insert id, but when using the SQLite database, you can get the last insert id by running the query:</p>

<blockquote>
<b>select last insert rowid</b>
</blockquote>
</body>
</html>
